Writing Java sourcecode in Emacs is a rather hard task. Emacs does not usually support the developer with lots of context awareness (friendly put…). What does the unsatisfied developer do when he is in need for functionality? He codes his own solution! So I have just started work on „Emacs Java Coding Extension (version 0.0.1)“. The project is admittedly in a very early phase. But there is an ambitious roadmap and some lines of code already available. Fresh from the scratch buffer where I tried it out:
(defvar mp:ac-classpath-cache nil) (defun mp:ac-classpath-init () (setq mp:ac-classpath-cache (mp:read-classes-from-jar))) (defvar ac-source-classpath '((prefix . "^import \\(.*\\)") (init . mp:ac-classpath-init) (candidates . mp:ac-classpath-cache))) (defun mp:read-classes-from-jar () (with-temp-buffer (call-process "/usr/bin/unzip" nil t nil "-l" "/home/map/opt/jdk1.8.0_101/jre/lib/rt.jar") (goto-char (point-min)) (let ((end 0) (result '()) (classname "")) (while (search-forward ".class" nil t nil) (end-of-line) (setq end (point)) (beginning-of-line) (goto-char (+ (point) 30)) (setq classname (substring (replace-regexp-in-string "/" "." (buffer-substring-no-properties (point) end)) 0 -6)) (setq result (cons classname result)) (forward-line 1) (beginning-of-line)) result)))
Put together correctly the code supplies a auto-complete source that knows about classes from the java rt.jar file – or any other jar file – or with some modifications several jar files.