forked from EnigmaCurry/emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ryan-java.el
314 lines (282 loc) · 12 KB
/
ryan-java.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Java mode customizations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'jde)
(setq jde-jdk-registry (quote (("1.6.0" . "/etc/opt/java"))))
(setq jde-jdk (quote ("1.6.0")))
(setq jde-complete-function (quote jde-complete-in-line))
(add-to-list 'auto-mode-alist '("\\.js\\'" . java-mode))
(setq author "Ryan McGuire")
;; Redefine RET to do special things in special circumstances
(define-key c-mode-base-map "\C-m" 'newline-and-indent)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; My custom skeletons
;; (Note: Good recipe for skeletons at
;; http://www.panix.com/~tehom/my-code/skel-recipe.txt)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Put the following in your .emacs so that the
;; abbrev table is set correctly in all modes.
;; (Not just for java)
;;
;; (add-hook 'pre-abbrev-expand-hook 'abbrev-table-change)
;; (defun abbrev-table-change (&optional args)
;; (setq local-abbrev-table
;; (if (eq major-mode 'jde-mode)
;; (if (jde-parse-comment-or-quoted-p)
;; text-mode-abbrev-table
;; java-mode-abbrev-table)
;; (if (eq major-mode 'python-mode)
;; (if (inside-comment-p)
;; text-mode-abbrev-table
;; python-mode-abbrev-table
;; ))))
;; )
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-abbrev java-mode-abbrev-table "psvm" "" 'java-skeleton-psvm)
(define-skeleton java-skeleton-psvm
"Insert a main function declaration" nil
"public static void main(String[] args){"
\n > _
\n "}" >
)
(define-abbrev java-mode-abbrev-table "print" "" 'java-skeleton-println)
(define-skeleton java-skeleton-println
"Insert a println statement" nil
"System.out.println("_");"
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; These are a bunch of adhoc skeletons I got from Spence Koehler.
;; Very useful.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(add-hook 'java-mode-hook
(function
(lambda()
(make-local-variable 'compile-command)
(setq compile-command (concat "buildone " buffer-file-name))
(let ((currentbuffer (current-buffer)))
;(ecb-activate)
(switch-to-buffer currentbuffer)
)
(setq c-basic-offset 2)
)))
(defun java-class-skeleton (&optional arg)
"Creates a java class skeleton in the current buffer (named *.java)"
(interactive "P")
(let ((path (split-string buffer-file-name "/")))
(insert "public class ")
; insert the last element on the path (filename) minus the ".java" part
(insert (substring (car (last path)) 0 -5))
(add-braces)
(execute-kbd-macro [?\C-a ?\C-p])
(add-javadoc-comment)
(add-package)
(insert "import java.util.*;\nimport java.io.*;\n")
(execute-kbd-macro [?\C-n ?\C-n ?\C-e ? ])
))
(defun java-test-class-skeleton (&optional arg)
"Creates a JUnit skeleton class in the current buffer (named *.java)"
(interactive "P")
(let ((path (split-string buffer-file-name "/")))
;let name be the last element on the path (filename) minus the ".java" part
(let ((name (substring (car (last path)) 0 -5)))
(insert "public class " name " extends TestCase")
(add-braces)
(insert "\npublic " name "(String name) {\n"
"super(name);\n"
"}\n"
"\n"
"public void testX() {\n"
"//Write your test here\n"
"}\n"
"\n"
"public static Test suite() {\n"
"TestSuite suite = new TestSuite(" name ".class);\n"
"return suite;\n"
"}\n"
"\n"
"public static void main(String[] args) {\n"
"junit.textui.TestRunner.run(suite());\n"
"}\n")
(mark-whole-buffer)
(c-indent-line-or-region)
(add-javadoc-comment)
(add-package)
(insert "import java.util.*;\nimport junit.framework.Test;\n"
"import junit.framework.TestCase;\n"
"import junit.framework.TestSuite;\n")
(execute-kbd-macro [?\C-n ?\C-n ?\C-e ? ])
)))
(defun add-braces (&optional arg)
"Inserts a pair of braces at the current cursor position."
(interactive "P")
(let ((macro
[?\C-e ? ?{ ?\C-m ?} ?\C-a ?\C-o ?\C-i]))
(execute-kbd-macro macro)))
(defun add-javadoc-comment (&optional arg)
"Inserts javadoc comments at the current cursor position."
(interactive "P")
;(push-mark)
(let ((pre-macro [?\C-i ?/ ?* ?* ?\C-m ?* ?\C-m ?* ? ?< ?p ?> ?\C-m ?* ? ?@ ?a ?u ?t ?h ?o ?r ? ])
(post-macro [?\C-m ?* ?/ ?\C-m ?\C-i]))
(execute-kbd-macro (vconcat pre-macro author post-macro))))
(defun add-package (&optional arg)
"Inserts a 'package' line at the top of the java file.
NOTE: It is assumed that the package is named beginning with an 'com.' directory."
(interactive "P")
(let ((preMacro [?\M-< ?\C-o]))
(execute-kbd-macro preMacro)
(insert "package ")
(insert (classpath2package (filepath2classpath buffer-file-name)))
(insert ";\n\n")
))
(defun classpath2package (classpath)
(let ((ppos (position-from-end ?. classpath)))
(if ppos (substring classpath 0 (position-from-end ?. classpath)) classpath)))
(defun filepath2classpath (filepath)
(let*
((sub0 (replace-regexp-in-string "/" "." filepath))
(orgpos (string-match "com\." sub0))
(thepos (if (null orgpos) (string-match "org\." sub0) orgpos))
(sub1 (substring sub0 thepos))
(sub2 (replace-regexp-in-string ".java" "" sub1)))
sub2))
(defun position-from-end (char string)
(let ((result nil)
(i (length string)))
(while (and (null result) (> i 0))
(setq i (1- i))
(if (eq (aref string i) char)
(setq result i)))
result))
(defun execute-java-file (&optional arg)
(interactive "P")
(let (
(command "java -Xmx640m ")
(test-command "java -Xmx640m junit.textui.TestRunner ")
;(command "j ")
;(test-command "jt ")
(javaclass (filepath2classpath buffer-file-name)))
(execute-kbd-macro [?\C-c ?s ?\M->])
(if (string-match ".Test" javaclass) (insert test-command) (insert command))
(insert javaclass)
))
(defun import-classes (&optional arg)
"Finds possible classes for the word at the current point, adding an 'import'
statement to the java file (interactively if more than one to choose)."
(interactive "P")
(let* ((name (thing-at-point 'word))
(paths-string
; note: relies on shell command "cpfinder name"
(shell-command-to-string
(concat "cpfinder " name)))
(len (length paths-string))
path)
(when (> len 0)
; return to point where we started when done
(save-excursion
; position point at end of imports or under "package"
(unless (re-search-backward "^import " nil t)
;go to beginning of buffer and down
(goto-char (point-min))
(forward-line 1)
)
(forward-line 1)
; deal with possibilities
(let ((paths (split-string paths-string "\n")))
(setq
path
(if (= (length paths) 1)
; only 1: don't need to ask
(insert-import paths "0")
; more than 1: create prompt with choices and ask
(let ((prompt "") choice (count 0))
(dolist (path paths)
(when (> (length path) 0)
(setq prompt (concat prompt "(" (number-to-string count) ") " path "\n"))
(setq count (+ count 1))))
(setq prompt (concat prompt "choice [0-" (number-to-string (- count 1)) "]: "))
(setq choice (read-no-blanks-input prompt))
(insert-import paths choice))))
))
; (ding)
(momentary-string-display "" (point) nil (concat "imported " path))
)))
(defun insert-import (paths choice)
"auxiliary to import-classes for inserting the 'import' statement"
(let* ((pos (string-to-number choice))
(path (elt paths pos))
(import (concat "import " path ";")))
;todo: don't import when in same package?
; only import if not already there
(unless (re-search-backward (concat "^" import) nil t)
(insert import)
(insert "\n"))
path))
(setq sandbox-name "/storage/subversion/sdnew/projects")
(setq sandbox-name-as-path (concat "/" sandbox-name "/"))
;; find project root
(defun find-project-root (filename &optional arg)
"Function to find the project root for the named file."
(let* ((sandbox-name-pos (string-match sandbox-name-as-path filename))
(next-slash-pos (string-match "/" filename (+ sandbox-name-pos (length sandbox-name-as-path)))))
(substring filename 0 next-slash-pos))
)
;; goto-file macro
(defun find-source (&optional arg)
"Macro to find the source files under the project root of the class at point."
(interactive "P")
(let ((pre-macro [?\M-b ?\C- ?\M-f ?\C-c ?c])
(project-root (find-project-root buffer-file-name))
(post-macro [?\C-c ?\C-f ?\C-m ?\C-y ?. ?j ?a ?v ?a ?\C-m]))
(execute-kbd-macro pre-macro)
(find-file-other-window project-root)
(execute-kbd-macro post-macro)))
;(fset 'find-source
; '[?\M-b ?\C- ?\M-f ?\C-c ?c ?\C-x ?4 ?f ?~ ?/ ?c ?o ?/ ?s ?e ?a ?r ?c ?h ?- ?s ?u ?b ?s ?y ?s ?\C-m ?\C-c ?\C-f ?\C-m ?\C-y ?. ?j ?a ?v ?a ?\C-m])
;; find-usages macro
(defun find-usages (&optional arg)
"Macro to find usages of the current symbol at point."
(interactive "P")
(let ((pre-macro [?\M-b ?\C- ?\M-f ?\C-c ?c])
(project-root (find-project-root buffer-file-name))
(post-macro [?\C-c ?f ?\C-m ?\C-y ?\C-m]))
(execute-kbd-macro pre-macro)
(find-file-other-window project-root)
(execute-kbd-macro post-macro)))
;(fset 'find-usages
; '[?\M-b ?\C- ?\M-f ?\C-c ?c ?\C-x ?4 ?f ?~ ?/ ?c ?o ?/ ?s ?e ?a ?r ?c ?h ?- ?s ?u ?b ?s ?y ?s ?\C-m ?\C-c ?f ?\C-m ?\C-y ?\C-m])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Keyboard shortcuts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(global-set-key "\C-cb" 'compile) ;C-c b <=> compile (build)
(global-set-key "\C-ce" 'execute-java-file)
(global-set-key "\C-cs" 'shell) ;C-c s <=> M-x shell
(global-set-key "\C-cc" 'clipboard-kill-ring-save) ;C-c c <=> copy
(global-set-key [f7] 'compile) ;<F7> <=> M-x compile
(global-set-key "\C-cC" 'clipboard-kill-ring-save) ;C-c C <=> copy
(global-set-key "\C-cl" 'goto-line) ;C-c l <=> goto-line
(global-set-key "\C-cq" 'query-replace-regexp) ;C-c q <=> query-replace-regexp
(global-set-key "\C-cr" 'replace-string) ;C-c r <=> replace-string
(global-set-key "\C-cf" 'find-grep-dired) ;C-c g <=> find-grep-dired
(global-set-key "\C-c\C-f" 'find-name-dired) ;C-c C-F <=> find-name-dired
(global-set-key "\C-cF" 'grep-find) ;C-c F <=> grep-find
(global-set-key "\C-cg" 'grep) ;C-c g <=> grep
(global-set-key "\C-cm" 'count-matches) ;C-c m <=> count-matches
(global-set-key "\C-ck" 'global-set-key) ;C-c k <=> global-set-key
(global-set-key "\C-cd" 'speedbar)
(global-set-key "\C-c\C-d" 'ediff-buffers) ;C-c C-d <=> ediff-buffers
(global-set-key "\C-c\C-i" 'indent-region)
(global-set-key "\C-cu" 'rename-uniquely)
(global-set-key "\C-c\C-l" 'run-lisp)
(global-set-key "\C-cB" 'browse-url-lynx-emacs)
(global-set-key "\C-c\C-j" 'show-only-java)
(global-set-key "\C-cj" 'add-javadoc-comment)
(global-set-key "\C-ci" 'add-braces)
(global-set-key "\C-cp" 'add-package)
(global-set-key "\C-cJ" 'java-class-skeleton)
(global-set-key "\C-cT" 'java-test-class-skeleton)
(global-set-key "\C-c-SPC" 'jde-complete-menu)
(global-set-key "\C-cG" 'find-source) ;C-c G <=> goto file
(global-set-key "\C-cU" 'find-usages)