Jump to content

Recommended Posts

Posted

2 questions on this one...

First off, I was wondering how to get the directory of the lisp being used, basically i need to read a file that will be relative to the directory of the lisp but for portability can't just set the directory to a default.

 

Second i've got a little function

(defun c:fopen()
(setq file (getstring "Enter filename:"))
   (if (= file "")
   (setq file (config_val "default_file" "section1"))
       (if (= (strpos file ":") nil) 
       (setq file (strcat (config_val "relative_dir" "section1") file))
       )
   )
(princ (strcat "\n" file))
(princ )
)

It keeps throwing a stringp error if I enter a filename of "test.txt", its odd because if i change

(setq file (strcat (config_val "relative_dir" "section1") file)) 

to

(setq file (config_val "relative_dir" "section1")) 

it outputs a string just fine, so i'm assuming the return of (config_val) must be a string as it passes the

 (princ (strcat "\n" file))

if it helps, the purpose of the function is to prompt the user for a filepath, if none is entered it defaults to the filepath retrieved from

(config_val "default_file" "section1")

(which works by the way), if no ":" is contained in the user input then it must not contain a drive so it'll use the user input combined with the relative path generate from

(setq file (strcat (config_val "relative_dir" "section1") file)) 

and finally if it does contain a ":" then it's assumed that its a full filepath and it leaves it alone

obviously some form of file open dialog would remove this problem, however its above my ability level at the moment so i'm improvising lol :D

Posted

figured out question 2 lol, simple answer

(getfiled )

however answers to the above questions are still welcome, good information to know regardless :)

Posted

What is this function: config_val? I've never heard/used it... :geek:

Posted

In reply to your PM, I shall post my contribution on the thread for others to benefit.

 

(defun getpath (lsp)
 (if (setq file (findfile lsp))
   (vl-filename-base file)))

 

Where "lsp" is the LISP filename.

 

But, obviously this assumes that the LISP file is in the search path.

Posted

Its user generated

(defun config_val(string header / found section val line file)
(setq file (open "C:\\Documents and Settings\\user\\Desktop\\ACAD\\lisp.conf" "r"))
(setq found nil)
(setq section nil)
   (while (and (/= (setq line (read-line file)) nil) (= found nil))
       (if (= section nil)
           (if(and (/= (strpos (strcase line) (strcase header)) nil) (= (substr line 1 1) "#"))
           (setq section T)
           )    
           (if (/= (strpos (strcase line) (strcase string)) nil)
               (progn
               (setq found T)
               (setq val (substr line (+ (strpos line "=") 1)))
               )
           )    
       )
   )
)

all it does is parse a file like

#Section1
value1=5
value2=6
value3=5
#Section2
default_dir=C:\

etc, u just feed it the section and the line bit and it'll return the value after the '=' sign

 

edit: thanks for the above bit, my next question then is how do i view/change the search path? i chose the pm method as i've been asking a lot of questions and didn't want to keep bumping my threads above everyone elses, but it probably is a pretty helpful answer :)

 

edit: also i realize that the function parses the file everytime, thats intentional as the people running the script aren't going to want to have to execute extra commands to 'refresh' the attributes set in the config file, they'd rather just make the changes to the file, save it, and then the changes will automatically take place, even in an active session

Posted

The search path is found by going to Tools > Options > Files (tab) > Search Path (top one me thinks!).

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...