Created
August 30, 2010 21:27
-
-
Save juergenhoetzel/558084 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defconst java-import-regex "^[[:space:]]*import[[:space:]]+\\(.*\\)\\.\\(.*\\);") | |
(defun clojure-java-import-line-parse (line) | |
(unless (string-match java-import-regex line) | |
(error "No import line")) | |
(let ((package (match-string-no-properties 1 line)) | |
(symbol (match-string-no-properties 2 line))) | |
(list package symbol))) | |
(defun clojure-java-import-region (start end) | |
"replace java import statements in text between point and mark with | |
corresponding Clojure namespace references" | |
(interactive (list (point) (mark))) | |
(unless (and start end) | |
(error "The mark is not set now, so there is no region")) | |
(let* ((lines (split-string (buffer-substring-no-properties start end) "[\n\r]+")) | |
(import-lines (remove-if-not (lambda (line) (string-match java-import-regex line)) lines)) | |
;; FIXME: no wildcard support in Clojure | |
;; http://groups.google.com/group/clojure/msg/ddca7ec56ea8b51f?pli=1 | |
(import-list (mapcar 'clojure-java-import-line-parse import-lines)) | |
;; wrap up common package imports in single import | |
(clj-import-list (reduce (lambda (alist c) | |
(if (string= (car (first alist)) | |
(car c)) | |
(cons (cons (car c) (append (cdr c) (cdr (first alist)))) (cdr alist)) | |
(cons c alist))) | |
import-list | |
:initial-value '()))) | |
(kill-region start end) | |
(insert-string (format "(:import%s)\n" (mapconcat (lambda (import-list) | |
(format "\t(%s)" (mapconcat 'identity import-list " "))) | |
clj-import-list "\n"))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Convert Java import statements to Clojure
If you convert Java code to Clojure you are often in need of converting a region of java import statements to corresponding Clojure namespace import references.
Mark the region:
Invoke interactive elisp function 'clojure-java-import-region: