You should be automatically redirected . If not, visit
http://newlisper.wordpress.com
and update your bookmarks.

31/07/2008

Looking for things

When writing newLISP, it's often useful to find any existing code that uses a particular function or symbol name. I'm a fan of both the Unix find command and the Mac's built-in search tool, Spotlight. The former is all-powerful, but it can be a little tricky to remember all those options. The latter is particularly good for finding text in documents - not just text files - since many developers provide a Spotlight system plug-in for searching their own document types.

Naturally, though, I also use a small newLISP utility alongside these two: its purpose is to look only in places where there are newLISP-related files. I reworked this recently based on some suggestions in the newLISP forum. Here's the code:

#!/usr/bin/env newlisp
(context 'Look4)

(constant 'extensions '("lsp" "txt" "html")) 

(set 'places 
  (list (string (env "HOME") "/projects/lisp")
        (string (env "HOME") "/projects/webapps")
        (string (env "HOME") "/projects/guiapps")
        (string (env "HOME") "/projects/tech-writing")
        "/usr/share/newlisp"))

(define (string-ends-with L str) 
  (exists (fn (x) (ends-with str x)) L))

(define (look-in-file pn)
 (let (contents "" res '())
  (when (string-ends-with extensions pn)
     (set 'file (open pn "r"))
     (while (search file (string "(.*?" Term ".{10,20})") true 1)
        (push $1 res -1))
     (close file)
     (if res (list pn res)))))

(define (Look4:Look4 dir)
   (dolist (nde (directory dir "^[A-z]"))
     (set 'item (append dir "/" nde))
     (if (directory? item)
       (Look4 item)
       (when (set 'r (look-in-file item Term)) (inc 'counter) (push r results)))))

(when (>= (length (main-args)) 2) 
  (set 'Term (main-args 2) 'counter 0)
  (map Look4 places)
  (when results
    (sort results)
    (dolist (r results)
        (println "\n" (r 0) "\n\t\t\t")
        (dolist (e (r 1)) (println "\t" e)))
        (println "Found " counter " occurrences of \"" Term "\"")))

(exit)

You can see that I've hardwired the names of the directories to search in. That's not very flexible, but it may suit your working style.

On the command line, you run the script and supply a string:

$ look4 unless

and you'll see the results in a couple of seconds:

...
/Users/me/projects/lisp/tokenizer.lsp

    (unless txt (exit))

/usr/share/newlisp/guiserver.lsp

;; a Piano instrument unless the function 'gs:mi

/usr/share/newlisp/modules/smtp.lsp

      (unless (and (empty?  user-

/usr/share/newlisp/util/nanorc

color blue "([[:space:]()]|^)(trim|true|true\?|unicode|unify|unique|unless|unpack|until|upper-
...

Found 201 occurrences of "unless"
$ 

Since the search is a regular expression one, it might be possible to supply a regex-friendly string, if you escape all the regex characters, but I find that I rarely use regular expressions in this type of search. I'm more likely to be asking "Didn't I once write a binary function?".

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home