问题
I want to jump through my java files by method, e.g. when I've got my anywhere, do a single keyboard shortcut to jump to the next end of a method or beginning of a method.
Emacs' "moving by defuns" with C-M-a and C-M-e is super-useful for C and does exactly what I want. But apparently in Java a defun is a whole class.
Moving by defuns: http://www.gnu.org/software/emacs/manual/html_node/emacs/Moving-by-Defuns.html
I've found that I can coerce C-M-f and C-M-b to sort of do what I want. They move forward and backward over any parentheses-balanced expression. The problem is that they only have the funcitonality I'm looking for when invoked from right outside the opening or closing brackets of a method definition, which is extremely limiting.
Expressions with Balanced Parentheses: http://www.delorie.com/gnu/docs/emacs/emacs_282.html
Any ideas would be welcome!
回答1:
imenu and speedbar are close to what you are looking for.
Otherwise you can define it by yourself. You can start with something like this:
(defvar java-function-regexp
(concat
"^[ \t]*" ; leading white space
"\\(public\\|private\\|protected\\|" ; some of these 8 keywords
"abstract\\|final\\|static\\|"
"synchronized\\|native"
"\\|[ \t\n\r]\\)*" ; or whitespace
"[a-zA-Z0-9_$]+" ; return type
"[ \t\n\r]*[[]?[]]?" ; (could be array)
"[ \t\n\r]+" ; whitespace
"\\([a-zA-Z0-9_$]+\\)" ; the name we want!
"[ \t\n\r]*" ; optional whitespace
"(" ; open the param list
"\\([ \t\n\r]*" ; optional whitespace
"\\<[a-zA-Z0-9_$]+\\>" ; typename
"[ \t\n\r]*[[]?[]]?" ; (could be array)
"[ \t\n\r]+" ; whitespace
"\\<[a-zA-Z0-9_$]+\\>" ; variable name
"[ \t\n\r]*[[]?[]]?" ; (could be array)
"[ \t\n\r]*,?\\)*" ; opt whitespace and comma
"[ \t\n\r]*" ; optional whitespace
")" ; end the param list
))
(defun my:next-java-method()
(interactive)
(re-search-forward java-function-regexp nil t)
)
(defun my:prev-java-method()
(interactive)
(re-search-backward java-function-regexp nil t)
)
Then bind my:next-java-method
and my:prev-java-method
to whatever key you want to go to the
来源:https://stackoverflow.com/questions/13127959/jump-through-java-methods-in-emacs