Code and Attachment Revised 2010.09.28Code and Attachment Revised 2010.09.30How do I AutoLoad or demand load lisp files as required. ??
I've seen this question dozens of times in forums,
I needed to make some changes to some really old code today so I thought I'd post it.
The file kdub-Auto-LispLoad.Lsp should be pre-loaded via any means that suits the way you work.
Refer to the comments in the code header.
The following type of code is added to either a .MNL file or any Lisp file that loads automatically.
;; Define Stubs to load routine Loaders
(kdub:Auto-LispLoad
"TestFunFile1"
'(("NoNoNo") ("TestFun3" 3) ("TestFun1") ("c:TestCmd1") ("TestFun2" 2))
)
(kdub:Auto-LispLoad "TestFun22" '(("TestFun22")))
(kdub:Auto-LispLoad "TestFun33" '(("TestFun33" 1)))
;; < etc>
As you can see the DemandLoader handles c:xx commands, functions with and without parameters and allows for multiple functions in one source file.
In the examples above, the following stubs are
auto-generated. into memory as follows ;
;;-------------------------------------------------
(Defun Testfun1 (/ Qfn)
(If (Setq Qfn (Kdub:Auto-Lispload_Findqualifiedfile "Testfunfile1"))
(Progn
(Princ "\nInitializing...\n")
(Setq Testfun1 Nil)
(Load Qfn)
)
(Progn
(Kdub:Auto-Lispload_Nofile "\"Testfunfile1\"")
(Setq Testfun3 Nil)
)
)
(If Testfun1
(Eval (Testfun1))
(Prompt (Strcat "\nerror : Unable To Evaluate " "Testfun1" "\n"))
)
)
;;
(Defun Testfun2 (Arg0 Arg1 / Qfn)
(If (Setq Qfn (Kdub:Auto-Lispload_Findqualifiedfile "Testfunfile1"))
(Progn
(Princ "\nInitializing...\n")
(Setq Testfun2 Nil)
(Load Qfn)
)
(Progn
(Kdub:Auto-Lispload_Nofile "\"Testfunfile1\"")
(Setq Testfun3 Nil)
)
)
(If Testfun2
(Eval (Testfun2 Arg0 Arg1))
(Prompt (Strcat "\nerror : Unable To Evaluate " "Testfun2" "\n"))
)
)
;;
(Defun Testfun3 (Arg0 Arg1 Arg2 / Qfn)
(If (Setq Qfn (Kdub:Auto-Lispload_Findqualifiedfile "Testfunfile1"))
(Progn
(Princ "\nInitializing...\n")
(Setq Testfun3 Nil)
(Load Qfn)
)
(Progn
(Kdub:Auto-Lispload_Nofile "\"Testfunfile1\"")
(Setq Testfun3 Nil)
)
)
(If Testfun3
(Eval (Testfun3 Arg0 Arg1 Arg2))
(Prompt (Strcat "\nerror : Unable To Evaluate " "Testfun3" "\n"))
)
)
;;
(Defun C:Testcmd1 (/ Qfn)
(If (Setq Qfn (Kdub:Auto-Lispload_Findqualifiedfile "Testfunfile1"))
(Progn
(Princ "\nInitializing...\n")
(Setq C:Testcmd1 Nil)
(Load Qfn)
)
(Progn
(Kdub:Auto-Lispload_Nofile "\"Testfunfile1\"")
(Setq Testfun3 Nil)
)
)
(If C:Testcmd1
(Eval (C:Testcmd1))
(Prompt (Strcat "\nerror : Unable To Evaluate " "C:Testcmd1" "\n"))
)
)
;;
;; and this one should spit the dummy because the function is not defined in the file.
;;-------------------------------------------------
(Defun Nonono (/ Qfn)
(If (Setq Qfn (Kdub:Auto-Lispload_Findqualifiedfile "Testfunfile1"))
(Progn
(Princ "\nInitializing...\n")
(Setq Nonono Nil)
(Load Qfn)
)
(Progn
(Kdub:Auto-Lispload_Nofile "\"Testfunfile1\"")
(Setq Testfun3 Nil)
)
)
(If Nonono
(Eval (Nonono))
(Prompt (Strcat "\nerror : Unable To Evaluate " "Nonono" "\n"))
)
)
;;-------------------------------------------------
Any subsequent use of any of these functions/Commands will Auto-load the correct source file and over-write the stub definition with the full definition from the file.
This can be tested by unzipping the sample files into a folder on your search path and progressively running each of these statements.
(vl-load-com)
;;Test .. ensure the Functions are not defined ... just for this test
(setq TestFun1 nil
TestFun2 nil
TestFun3 nil
TestFun22 nil
c:TestCmd1 nil
TestFun33 nil
nonono nil
)
;; Define Stubs to load routine Loaders
(kdub:Auto-LispLoad
"TestFunFile1"
'(("NoNoNo") ("TestFun3" 3) ("TestFun1") ("c:TestCmd1") ("TestFun2" 2))
)
(kdub:Auto-LispLoad "TestFun22" '(("TestFun22")))
(kdub:Auto-LispLoad "TestFun33" '(("TestFun33" 1)))
;; If you highlight and right click on the functionName in the VLIDE then select [b]Inspect...[/b] you'll see that the functioin is defined.
;; Something like this will be returned #<USUBR @0000000035984bd8 TESTFUN2>
;; Run a routine
(TestFun1)
(TestFun2 100 "Apples")
(setq mmage 82)
(TestFun3 "Mickey" "Mouse" mmage)
(c:TestCmd1)
(nonono)
(TestFun22)
(TestFun33 2)
;;-------------------------------------------------
... you'll see that each file is loaded as any function in it is required. Further calls to that function (or any others in the file) are not reloaded because the stub definition has been over-written by the full definition.
kdub-Auto-LispLoad.LSP
(vl-load-com)
;;-------------------------------------------------
;;-------------------------------------------------
(defun kdub:Auto-LispLoad_FindQualifiedFile (f / fn)
;; CodeHimBelonga kdub 2001.07.28
;; Revised 2010.09.29
(if (or (setq fn (findfile f))
(setq fn (findfile (strcat f ".VLX")))
(setq fn (findfile (strcat f ".FAS")))
(setq fn (findfile (strcat f ".LSP")))
)
(vl-string-translate "\\" "/" fn)
)
)
;;-------------------------------------------------
(defun kdub:Auto-LispLoad_NoFile (FileName)
;; CodeHimBelonga kdub 2001.07.28
;; Revised 2010.09.27
(alert
(strcat
"The requested file " FileName " "
(if (not (vl-filename-extension FileName))
"(.lsp/.fas/.vlx) "
)
"\nwas not found in the Profile search path folders.\n"
"\nPlease check the installation of the support files"
"\nand 'Options->Files->Path."
)
)
(princ)
)
;;-------------------------------------------------
;;-------------------------------------------------
(defun kdub:Auto-LispLoad (FileName ParamList /)
;; CodeHimBelonga kdub 2001.07.28
;; based on sample code from VitalLisp by Basis Software circa 1999
;; Revised 2010.09.27
;; Updated [Build:2010.09.29-2] (bugs and redundancies)
;;-------------------------------------------------
;; <fileName> - either
;; fully qualified Path/Name/extension
;; or Path/Name {no ext}
;; or Name {no ext}
;; <ParamList> a list of lists representing :-
;; 1) A cons List of FunctionName string and Argument Count
;; eg: ("TestFun2" 2)
;; 2) A list containing the FunctionName string
;; eg: ("TestFun3" )
;; Example : (kdub:Auto-LispLoad "TestFunFile1"
;; '(("TestFun3" 3) ("TestFun1") ("c:TestCmd1")))
;;-------------------------------------------------
;;
(Setq FileName (strcat "\"" (vl-string-translate "\\" "/" FileName) "\""))
;;
(mapcar
(function
(lambda (_cmd / CommandName a ArgS)
(setq CommandName (Read (car _cmd))
a -1
ArgS '()
)
(if (not (vl-symbol-value CommandName))
(progn
(if (setq argsQty (cadr _cmd))
(setq Args
(reverse
(While (< (Setq a (1+ a)) argsQty)
(Setq Args (cons (read (Strcat "Arg" (Itoa a))) Args))
)
)
)
)
(eval
(list
'defun
CommandName
(append Args '(/ qfn))
(list
'if
(list
'setq
'qfn
(list 'kdub:Auto-LispLoad_FindQualifiedFile (read FileName))
)
(list 'progn
'(princ "\nInitializing...\n")
(list 'setq CommandName 'nil)
(list 'load 'qfn)
)
;; stub else
(list 'progn
(list 'kdub:Auto-LispLoad_NoFile FileName)
(list 'setq CommandName 'nil)
)
)
(list 'if
CommandName
(list 'eval (cons CommandName Args))
;; stub else
(list 'prompt
(list 'strcat
"\nError : Unable to evaluate "
(vl-prin1-to-string CommandName)
"\n"
)
)
)
)
)
)
)
)
)
ParamList
)
(princ)
)
;;-------------------------------------------------
;;-------------------------------------------------
(princ)
[edit:kdub] Attachment Revised 2010.09.28
[edit:kdub] Attachment Revised 2010.09.30 [Build:2010.09.29-2]