Open lisp and search for string

Open lisp and search for string

vishshreevT578L
Advocate Advocate
4,312 Views
29 Replies
Message 1 of 30

Open lisp and search for string

vishshreevT578L
Advocate
Advocate

Hi.....

 

I am looking for source code which will open lisp file and search for prompted string and output will be files name.

 

For example.....

 

(DEFUN GETINFO ()
  (SUPERMAN (IF SPIDERMAN T NIL))
)

 

My superman function is somewhere in the folder which contains multiple lisps and I want to look for the same function to load it.

 

 

Thanks in advance.

Shreev
0 Likes
Accepted solutions (2)
4,313 Views
29 Replies
Replies (29)
Message 2 of 30

jwhite
Advocate
Advocate

You may want to let Windows do the searching for you. You need to add the .lsp extension to the file types that are indexed for contents. I don't remember how to do it but a Google search for file indexing for your version of Windows should find something. I use it every so often to find that lisp file that used a command I rarely use.

0 Likes
Message 3 of 30

hencoop
Advisor
Advisor

You should use the VLIDE to search for your string within files.  Once the filename is determined, add code to your routine(s) that will automatically load it before it is to be used.

 

; The second argument of the LOAD function will tell you if the file is not loaded for any reason.
(IF <function> NIL (LOAD <filename> "\nFile <filename> not loaded! ")) ; Load the function
; if <filename> is in your Support File Search Path you do not need to include its path in the LOAD function. (<function>) ; Execute the function
; <function> and <filename> including < and > must be replaced with your specific string values.

 

Searching so broadly for a string within all of your lisp files is very time consuming.  Each file must be opened and each line must be read to find the match. It is possible to find the same string in more than one file and you can't easily determine which one is what you are looking for.  Once you have found the name of what you are looking for once you don't want to have to ever find it again.

AutoCAD User since 1989. Civil Engineering Professional since 1983
Product Version: 13.6.1963.0 Civil 3D 2024.4.1 Update Built on: U.202.0.0 AutoCAD 2024.1.6
                        27.0.37.14 Autodesk AutoCAD Map 3D 2024.0.1
                        8.6.52.0 AutoCAD Architecture 2024
0 Likes
Message 4 of 30

steve_carson
Enthusiast
Enthusiast

This isn't exactly what you asked for but may accomplish your goal. With the help of some Lee Mac functions I wrote this to search a folder for all lsp and mnl files and print to a text file all of the functions, separated by filename. Just open this lisp file and modify the text file name and path to be whatever you want and load it. Loading the file runs it.

 

HTH

0 Likes
Message 5 of 30

phanaem
Collaborator
Collaborator

Try the attached lisp. I wrote it to find a specific text inside my lisp files.

You can adapt it to load the files.

 

(defun search_lsp (str dir sd / file found l line)
  (foreach x (vl-directory-files dir "*.lsp" 1)
    (setq file (open (strcat dir x) "r")
          found nil)
    (while
      (and
        (not found)
        (setq line (read-line file))
      )
      (setq found (wcmatch (strcase line) (strcase str)))
    )
    (if found (print (strcat dir x)))
    (close file)
  )
  (if sd
    (foreach subdir (cddr (vl-directory-files dir "*" -1))
      (search_lsp str (strcat dir subdir "\\") sd)
    )
  )
)

(search_lsp "*acobj*" "D:\\Lisp\\" T)

The search is not case sensitive and you can use wildcards.

The second argument is the start directory. Note it has to end with "\\".

The 3-rd argument is T or nil, T if you want to search in sub-directories.

Message 6 of 30

scot-65
Advisor
Advisor

nice!

 


Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes
Message 7 of 30

john.uhden
Mentor
Mentor

It's not time consuming if you use ZTree.  You simply set the file spec to *.lsp,*.mnl, log as many directories as you wish, hit S (for Showall) and do a Ctrl+T (tag) and Ctrl+S (search) for the the string "defun superman."  ZTree is cheap too.  Plus it let's you view files without opening them and gather lines of text and copy to clip board, and many other things like changing the date and/or time and name and attribute of multiple files at once.  You can copy selected files and even entire directories to anywhere else.  I even have mine set up to ZIP selected files to wherever I want.  Yes, you can select a file and hit O (for Open) if the extension is associated with an installed application.  Plus, if you had some DOS experience, you can hit X (for eXecute) and type in a DOS command like "SET|MORE." to check out your environment.

 

I would love it if you young dogs could learn some old tricks.

John F. Uhden

0 Likes
Message 8 of 30

vishshreevT578L
Advocate
Advocate

Its not working

Shreev
0 Likes
Message 9 of 30

pbejse
Mentor
Mentor

@vishshreevT578L wrote:

Its not working


Mind if i ask how did you go about running the posted code? Any error message?  if you don' use wildcards, it will explicitly look for the exact match.

I wont find the string if you' re looking for "banana" and the closest match is "Give me the  banana ", but  "*banana*" 

 

What it's not going to do is load the file if found, it will only print the path(s) on the command line. I had something similar to this long ago. to search for the embedded date (getvar  'CDATE ) . and i think some of you may know the reason for that.

 

 

 

0 Likes
Message 10 of 30

vishshreevT578L
Advocate
Advocate

I think my question here is not that much clear, so to make you understand i would like to go in detail,

1) I am trying to work on the current code which is as below,

 
(defun c:drawaline ()
  (vl-load-com)
  (a)   ;This line is my querry
  (setq x (getpoint))
  (setq y (getpoint x))
  (command "line" x y)
  (princ)
  )


2) Now i have a folder containing nos of lisp files.

For example;

a.lsp
b.lsp
c.lsp
d.lsp etc.,


3) a.lsp file contains

(defun test ()
  (body of program)
  (princ)
)

(defun test1 ()
  (body of program)
  (princ)
)

(defun test2 ()
  (body of program)
  (princ)
)

(defun a ()
  (prompt "click points to draw a line")
  (princ)
  )


4) Here i am looking for a function "(a)" which is written somewhere in the folder which contains nos of lisp file.

5) I need a program which will ask me for a string "what are you looking for? suppose i provide "defun a" so it will open all the lisp files from the selected folder and search for the provided string and will return "your result is in the "a.lsp" file.

I hope i have made the right question.

Thanks in advance...............

Shreev
0 Likes
Message 11 of 30

pbejse
Mentor
Mentor

Use the same code shown at post#5 and tweak it a bit to load the file instead of printing on command line AND  modify it to stop evaluation as soon  the "string match" is found,  unless its something unique like "*borkasdmaicus*"

 

You need help with that?

 

 

 

 

 

 

 

 

 

0 Likes
Message 12 of 30

john.uhden
Mentor
Mentor

ZTree...  F "*.lsp";Ctrl+T; Ctrl+S "defun a*"  Then view and pick which file you want to open.

That's the kind of answer you get from old dogs.

 

But I suppose you want us to build you a LSP routine to look through every folder in every drive for any LSP file that contains "(defun a"

I could do it, but I don't have any need for it.  Plus, I now have a regular job!  YAY!

My apologies for sounding so grumpy.

John F. Uhden

0 Likes
Message 13 of 30

john.uhden
Mentor
Mentor

Be careful.  I think *borkasdmaicus* is a Slobovian curse word.

John F. Uhden

0 Likes
Message 14 of 30

vishshreevT578L
Advocate
Advocate

Good Morning Sir......

 

(defun a ()
  (prompt "click points to draw a line")
  (princ)
  )

 

I have a lisp file which contains the above code "a.lsp" in this location "C:\Users\GAW81683\Desktop\shreev"

 

when I run your function I get the result nil

 

(search_lsp "defun a" "C:\Users\GAW81683\Desktop\shreev" T)

 

error image is attached.

 

 

Shreev
0 Likes
Message 15 of 30

pbejse
Mentor
Mentor

You are missing "\\"  at the end of the path argument

 

 

0 Likes
Message 16 of 30

vishshreevT578L
Advocate
Advocate

STILL I AM GETTING NIL

Shreev
0 Likes
Message 17 of 30

vishshreevT578L
Advocate
Advocate

CAN YOU TRY THE SAME AT YOUR SIDE

Shreev
0 Likes
Message 18 of 30

pbejse
Mentor
Mentor

@vishshreevT578L wrote:

CAN YOU TRY THE SAME AT YOUR SIDE


If you read my reply at post # 9,

 

".... if you don' use wildcards, it will explicitly look for the exact match.... wont find the string if you' re looking for "banana" and the closest match is "Give me the  banana ", but  "*banana*"  

 

In your case, (search_lsp "defun a" "C:\Users\GAW81683\Desktop\shreev" T), it's searching for "defun a" from the result of read-line function. Now if the lsp file is structured like this

 

 

(
defun a
( / b c d )
(do something)
)

Then your search will yield a result.

 

 

(setq line (read-line file))

will give you "defun a", but with this

 

(defun a ( / b c d )
(do something)
)

will evaluate to nil , line variable value is "(defun a ( / b c d )" which is NOT a match to "defun a"

 

try (search_lsp "defun a*" "C:\Users\GAW81683\Desktop\shreev" T)

 

Hang on a minute, i will write a short routinize that may help you.

 

0 Likes
Message 19 of 30

vishshreevT578L
Advocate
Advocate

Alright Sir....

 

Awaiting for the perfect match..........

Shreev
0 Likes
Message 20 of 30

pbejse
Mentor
Mentor
Accepted solution

I modify the code to stop when the target lisp is found.

 

(defun search_lsp (str dir / file found l line f targetdir)
(setq targetdir  (vl-directory-files dir "*.lsp" 1))
	(while (and (null f)
	            (setq x (car targetdir)))
		 (setq file (open (strcat dir x) "r")
		          found nil)
		    (while
		      (and
		        (not found)
		        (setq line (read-line file))
		      )
		      (setq found (wcmatch (strcase line) (strcase str)))
		    )
	    	(if found (setq f (strcat dir x)))
	    (close file)
              (setq targetdir (Cdr targetdir))
	  )
    f 
)

 

AND from your example

 

 

(defun c:drawaline ( / opt theprogram x y)
  (vl-load-com)
      (initget "a b c")
      	(setq opt (getkword "\nwhat are you looking for [a/b/c]?: "))
      	(if (and (setq theprogram (search_lsp (strcat "(defun " opt "*") "C:\\Users\\GAW81683\\Desktop\\shreev\\"))
                (or (eq (type (eval (read opt))) 'USUBR) (load theprogram))
                )
            	(progn
			(setq x (getpoint "\nPick Point"))
			(setq y (getpoint "\nPick Next Point"))
			  (command "line" x y)
                      )
            )  
  (princ)
  )

HTH

0 Likes