Skip to content

Instantly share code, notes, and snippets.

@skeeto
Created July 25, 2012 21:21
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save skeeto/3178747 to your computer and use it in GitHub Desktop.
Save skeeto/3178747 to your computer and use it in GitHub Desktop.
Automatically open .class files in Emacs with javap
(add-to-list 'file-name-handler-alist '("\\.class$" . javap-handler))
(defun javap-handler (op &rest args)
"Handle .class files by putting the output of javap in the buffer."
(cond
((eq op 'get-file-buffer)
(let ((file (car args)))
(with-current-buffer (create-file-buffer file)
(call-process "javap" nil (current-buffer) nil "-verbose"
"-classpath" (file-name-directory file)
(file-name-sans-extension (file-name-nondirectory file)))
(setq buffer-file-name file)
(setq buffer-read-only t)
(set-buffer-modified-p nil)
(goto-char (point-min))
(java-mode)
(current-buffer))))
((javap-handler-real op args))))
(defun javap-handler-real (operation args)
"Run the real handler without the javap handler installed."
(let ((inhibit-file-name-handlers
(cons 'javap-handler
(and (eq inhibit-file-name-operation operation)
inhibit-file-name-handlers)))
(inhibit-file-name-operation operation))
(apply operation args)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment