;; This code was written by Alexander Kitaev and Dmitry Barashev ("Authors")
;; This code is public domain. You can use it for any purposes and
;; modify the way you want.
;; Authors are not responsible for anything bad that may happen with you, you code
;; or your hardware when you use this code
;; We hope you find this code helpful :)
;; INSTALL:
;; Either place this code into your .emacs or make it accessible for Emacs otherwise.
;; E.g. add hook to java file opening.
;; In order to be able to compile java file opened in the current buffer you need
;; to define the following target in your ANT buildfile:
;;
;;
;;
;;
;;
;;
;; This script will invoke target "compile_file" passing relative path of the currently opened file in ${fileName} property.
;; You should set value of ${src.dir} property so that in conjunction with ${fileName} it would give you a valid full path.
;; E.g. if your java sources are located in "/prefix/com/foo/bar" directory and have package name "com.foo.bar" then you
;; should set value of ${src.dir} to "/prefix"
;;
;;
;; define name of the buildfile to search for
(defvar ant-build-file "build.xml")
;; This function compiles file in the current buffer. It calculates path of the file relative to java source code tree root
;; (calculation is based on package statement) and then calls function (ant ...) passing "compile_file" as first parameter
;; and "-DfileName= as a second one
(defun ant-compile-file()
(interactive)
(if (buffer-file-name)
(ant "compile_file" (concat "-DfileName=" (concat (get-java-source-dir (current-buffer)) "/" (file-name-nondirectory (buffer-file-name)))))
)
)
;; This function composes ant command line and runs it in compilation buffer
;; This function can be called in interaction mode (it will ask name of the target to run in this case)
(defun ant(target &optional parameters)
(interactive (list (setq ant-target "") (read-from-minibuffer "Target : " ant-target)))
(setq ant-target target)
(compile (ant-compose-command ant-target parameters))
)
;; This function composes and command
(defun ant-compose-command(ant-target &optional ant-parameter)
(concat "ANT " "-emacs" " -find " ant-build-file " " ant-target " " ant-parameter)
)
;; This function calculates a relative path of the file in the current buffer. Path is relative to
;; java source code directory root. Function takes necessary information from package statement.
;; If package statement is "package com.foo.bar" and filename is Class1.java then return value is
;; com/foo/bar/Class1.java
(defun get-java-source-dir(class-buffer)
(interactive (list (setq class-buffer (current-buffer))))
(save-excursion
(goto-char 0)
(setq package-statement-start (search-forward "package" nil t))
(setq package-statement-end (search-forward ";" nil t))
(if (and (not (eq package-statement-start nil))
(not (eq package-statement-end nil)))
(save-excursion
(setq package-statement-start (+ package-statement-start 1))
(setq package-statement-end (- package-statement-end 1))
(setq parts (split-string
(buffer-substring package-statement-start package-statement-end) "[\.]+"))
(setq package-statement "")
(while (car parts)
(if (not (string-equal "" package-statement))
(setq package-statement (concat package-statement "/" (car parts)))
(setq package-statement (car parts))
)
(setq parts (cdr parts))
)
(elt (list package-statement) 0)
)
(elt (list nil) 0)
)
)
)