Hi,

I managed to put together an Emacs Lisp function to automatically
declare all defined symbols in a Clojure source file. The algorithm is
crude: it doesn't understand syntax, it just  parses lines beginning
with either "(def  " or "(defn ". A single line beginning with
"(declare " macro must already be present.

I wasn't able to match syntax of Clojure symbols inside the regex - it
should have been "(syntax symbol)", shouldn't it?  - therefore I
resorted to:

(any "-" "a-z" "A-Z" "0-9")

which is incomplete.

You could hook the function to file saving or something like that. It
works for me.

Cheers


(require 'cl)
(require 'rx)

;; WARNING: IMPLEMENTED BY A NEWBIE. NO WARRANTIES!
(defun clj-auto-declare ()
        (interactive "*") ;; Buffer contents are going to change.
        (save-excursion
                (save-restriction
                        (save-match-data
                                ;; Scan buffer for defined symbols.
                                (widen)
                                (goto-char (point-min))
                                (let ((defined-symbols '()))
                                        (while (re-search-forward (rx 
line-start "(" (or "def" "defn")
                                                                                
                  (one-or-more space)
                                                                                
                  (group (one-or-more  (any "-" "a-z" "A-Z" "0-9"))))
                                                                                
          nil t)
                                                (push (match-string 1) 
defined-symbols))
                                ;; Handle found symbols.
                                (if defined-symbols
                                                ;; Scan buffer for 'declare' 
and update it.
                                                (progn (goto-char (point-min))
                                                           (unless 
(re-search-forward (rx line-start "(declare"
                                                                                
                                          (group (zero-or-more (or space (any 
"-" "a-z" "A-Z"
"0-9"))))
                                                                                
                                          ")")
                                                                                
                                  nil t)
                                                                   (error 
"Global 'declare' not found. Have you entered it as
a single line?"))
                                                           (let 
((uptodate-declare (concat "(declare " (mapconcat
'identity defined-symbols " ") ")")))
                                                                   
(replace-match uptodate-declare)
                                                                   (message 
"Declarations have been updated.")))
                                        (message "Nothing to declare, 
sir.")))))))


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to