In the project I'm working on the package name in a java file is often
wrong (don't ask why!) so I wrote a little function to check the
package name and fix it if it is wrong. I've added this to my
jde-mode-hooks and it seems to work fine. I'm posting it to share and
to solicit comments. Two "problems" with it are:
1. When I visit a file using tags the message is lost amongst other
minibuffer verbage.
2. My domain name is hardwired in. I realize I can make this a
defconst/defvar, but is there something better?
3. I'm not an elisp wizard, improvements welcome.
;; Check the package of the current buffer. If it is wrong update it.
(defun check-java-package ()
(interactive)
(save-excursion
(let* ((used-package (jde-parse-get-package-name))
(cwd (file-name-directory (buffer-file-name)))
(dots (substring (subst-char-in-string ?/ ?. cwd) 0 -1))
(tail (second (split-string dots "com.cimsoft")))
(actual-package (concat "com.cimsoft" tail)))
(beginning-of-buffer)
(if (and (not (string-equal used-package actual-package))
(re-search-forward "package[ \t]+\\(.*\\)[ \t]*;" nil t))
(progn
(replace-match actual-package t nil nil 1)
(message "Warning: Package name updated."))))))
(add-hook 'jde-mode-hook '(check-java-package))
--
Robert