public
Created

A minor mode for compiling and running java from emacs

  • Download Gist
javac-mode.el
Emacs Lisp
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
;; Minor mode for compiling java classes
(defvar javac-mode nil
"Mode variable for java class files.")
(make-variable-buffer-local 'javac-mode)
 
(defun javac-mode (&optional arg)
"Minor mode for compiling java class files.
Special Commands:
\\{javac-mode-map}"
(interactive "P")
(setq javac-mode
(if (null arg) (not javac-mode)
(> (prefix-numeric-value arg) 0)))
;; Set the minor mode alist to "javac" when the mode is active
(if (not (assq 'javac-mode minor-mode-alist))
(setq minor-mode-alist
(cons '(javac-mode " javac")
minor-mode-alist)) )
;; define a variable for the keymap
(defvar javac-mode-map nil
"Key map for javac minor mode.")
;;(setq javac-mode-map nil)
;; Load up the javac map
;;(if javac-mode-map ;; reminder: when adding new define-key,
;; be sure to clear javac-mode-map in order for
;; the new keys to take effect!
;; nil ;; do nothing if javac-mode-map exists
;; erase any currently defined javac-mode-map
(setq javac-mode-map nil)
(setq javac-mode-map (make-sparse-keymap)) ;; create the map
(define-key javac-mode-map "\C-cc" 'javac-compile-class) ;; compile java into .class file
(define-key javac-mode-map "\C-cd" 'javac-add-dir-to-javac-classpath) ;; add a dir to the classpath
(define-key javac-mode-map "\C-cj" 'javac-run-java) ;; run java on a class
 
(use-local-map javac-mode-map) ;; load up the keymap
 
(defvar javac-command nil
"String used as argument to shell-command for compiling java")
;; classpath
(defvar javac-classpath "."
"String used to store the javac-classpath")
(defun javac-compile-class ()
"Compile a .java file into a .class file"
(interactive)
(setq javac-command (concat "javac -cp " javac-classpath " " buffer-file-name)) ;; compile the file associated with the local buffer
(shell-command javac-command)
;; end of javac-compile-class
)
(defun javac-run-java ()
"Run the command line java on a .java file as a class with the curent javac-classpath"
(interactive)
(setq class-name (split-string buffer-file-name "\\/" )) ;; split up the filename to remove dir information
(setq class-name (split-string (car (last class-name)) "\\.")) ;; using the last element of class-name, split by .
;; car gets the first element of the list and returns the string
(setq class-name (pop class-name)) ;; finally, get the name of the class
(setq javac-command (concat "java -cp " javac-classpath " " class-name)) ;; create the java command for the buffer
(shell-command javac-command) ;; run the command
)
(defun javac-add-dir-to-javac-classpath ()
"Add a directory to the current javac-classpath.
This variable will be saved for future emacs sessions."
(interactive)
(defvar temp-dir nil
"Temporary variable for storing a new javac-classpath dir.")
(setq temp-dir
(read-directory-name "Dir to add to javac-classpath: "))
(list temp-dir)
(setq javac-classpath (concat javac-classpath ":" (expand-file-name temp-dir)))
;; save the javac-classpath between sessions
(customize-save-variable 'javac-classpath javac-classpath)
;; end of javac-add-dir-to-javac-classpath
)
 
;; end of javac-mode
)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.