-
Notifications
You must be signed in to change notification settings - Fork 3
/
include-lib.LSP
724 lines (692 loc) · 23.9 KB
/
include-lib.LSP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
;;;=====================================================================
;;; Please load this LISP file to your autolisp development
;;; enviroment. Then the following function is declared.
;;; Please see also include.LSP.
;;;
;;; Use the following functions depending on your development
;;; environment.
;;;
;;; ----------------------+-------------------------
;;; Visual LISP | Visual Studio CODE
;;; | or Visual LISP
;;; ----------------------+-------------------------
;;; include | include
;;; | load-file
;;; | resource-file
;;; | createPRV
;;; exportResource |
;;; (or exportLib) |
;;; ----------------------+-------------------------
;;;
;;;=====================================================================
;;; (include 'function filename)
;;;---------------------------------------------------------------------
;;; function : symbol
;;; filename : string
;;;---------------------------------------------------------------------
;;; If no function is defined, the file will be loaded. The loaded
;;; file name is stored in *include:LibraryFiles* .
;;; This function is used to load a file from the library, if needed.
;;;=====================================================================
;;; (load-file filename)
;;;---------------------------------------------------------------------
;;; filename : string
;;;---------------------------------------------------------------------
;;; The file is loaded and the name is stored in *include:LoadFiles* .
;;; This function can be used to load your project's files from Visual
;;; Studio Code instead of the load function in AutoCAD 2021 and later.
;;; This is used in combination with the createPRV function.
;;;=====================================================================
;;; (resource-file filename)
;;;---------------------------------------------------------------------
;;; filename : string
;;;---------------------------------------------------------------------
;;; The file name is stored in *include:ResourceFiles* .
;;; This function remembers the name of the resource file so that it
;;; can be used by the createPRV function. It has no direct effect on
;;; the execution of your program. Specify a DCL or TXT file with this
;;; function.
;;;=====================================================================
;;; (createPRV obj-temp target-folder)
;;;---------------------------------------------------------------------
;;; obj-temp : string or nil (If nil, the default value is "temp")
;;; target-folder : string or nil (If nil, the default value is "")
;;;---------------------------------------------------------------------
;;; This function creates the VLX application make file (PRV) for
;;; Visual LISP or command MAKELISPAPP. This configuration file contains
;;; the contents of *include:LoadFiles*, *include:LibraryFiles* and
;;; *include:ResourceFiles*.
;;; The AutoCAD 2021 and later commands MAKELISPAPP enables the
;;; creation of VLX applications without the use of VisualLISP projects.
;;;=====================================================================
;;; (exportResource)
;;;---------------------------------------------------------------------
;;; This function writes out a project file that can be used with
;;; Visual LISP. The project file is the contents of *include:LibraryFiles*
;;; loaded with the include function.
;;; This project file can be included as a resource in the Visual LISP
;;; VLX application definition file(PRV). In principle, this function
;;; should be used if you are developing a program using Visual LISP
;;; instead of Visual studio Code and MAKELISPAPP.
;;;=====================================================================
;;; (exportLib fas-temp temp)
;;;---------------------------------------------------------------------
;;; fas-temp : string or nil (If nil, the default value is "temp")
;;; temp : string or nil (If nil, the default value is "temp")
;;;---------------------------------------------------------------------
;;; The basic behavior of this function is the same as the exportResource
;;; function. You can change the content a little bit, depending on the
;;; argument.
;;; The fas-temp and temp arguments specify a temporary folder.
;;; (exportLib nil nil) is the same as (exportResource).
;;;=====================================================================
;;; +------------------------------------------------------------------+
;;; Copyright (c) 2020 manual chair japan
;;; Released under the MIT license
;;; https://opensource.org/licenses/mit-license.php
;;; +------------------------------------------------------------------+
(setq *DrawingLevelEnviromet* T)
;;;*******************************************************************
;;;
;;; support functions
;;;
;;;*******************************************************************
(setq *include:LoadFiles* nil
*include:LibraryFiles* nil
*include:ResourceFiles* nil
*include:symbol-table* nil
*include:loading-stack* nil ;;
;; *include:message-mode* = 0~ NOTICE, COUTION and ERROE
;; 10~ COUTION and ERROE
;; 100~ ERROR only
*include:message-mode* 0
*include:working-folder* nil
)
(defun include:message (level message)
(if (<= *include:message-mode* level)
(princ message)
)
nil
)
(defun include:replace-to-slash (path)
(if path
(vl-list->string
(mapcar
(function
(lambda (char)
(if (= char 92 ;|(ascii "\\")|;)
47
;|(ascii "/")|;
char
)
)
)
(vl-string->list path)
)
)
)
)
(defun include:normalize-path (path last-slash / last-char)
(if (/= path "")
(progn (setq last-char (vl-string-elt path (1- (strlen path))))
(if (= last-char 47 ;|(ascii "/")|;)
(if last-slash
path
(substr path 1 (1- (strlen path)))
)
(if last-slash
(strcat path "/")
path
)
)
)
path
)
)
(defun include:make-list (size initial-element)
(if (not (zerop size))
(cons initial-element
(include:make-list (1- size) initial-element)
)
)
)
(defun include:trySearch:sub (extensions)
(if extensions
(if (setq full-filename (findfile (strcat filename "." (car extensions))))
full-filename
(include:trySearch:sub (cdr extensions))
)
)
)
(defun include:trySearch (filename extensions)
(include:trySearch:sub extensions)
)
(defun include:full-filename (filename extension-serach / full-filename path)
(include:replace-to-slash
(if (not (null (setq full-filename (findfile filename))))
full-filename
(if extension-serach
(include:trySearch
(if (/= (setq path (vl-filename-directory filename)) "")
(strcat path "/" (vl-filename-base filename))
(vl-filename-base filename)
)
'("VLX" "FAS" "LSP")
)
)
)
)
)
(defun include:file-info (filename extension-serach load-file-symbol / file-info
full-filename
)
(if (setq full-filename (include:full-filename filename extension-serach))
(cons (strcase filename)
(list (read (substr (vl-filename-extension full-filename) 2))
full-filename
)
)
)
)
(defun include:load-file ($_filename $_load-list-symbol / $_file-info $_list-item
$_result
)
(if
(setq $_file-info ;_ Check for file availability
(include:file-info $_filename T $_load-list-symbol)
)
(if
(vl-catch-all-error-p
(setq $_result (vl-catch-all-apply
'load
(list (cadr (cdr $_file-info)))
)
)
)
(progn
(include:message
100
(strcat "\n; ERROR : An error occurred while loading "
$_filename
" ."
)
)
(exit)
)
(progn
(set $_load-list-symbol
(if
(setq $_list-item (assoc (strcase $_filename)
(vl-symbol-value $_load-list-symbol)
)
)
(subst $_file-info
$_list-item
(vl-symbol-value $_load-list-symbol)
)
(cons $_file-info (vl-symbol-value $_load-list-symbol))
)
)
$_result
)
)
(progn
(include:message
100
(strcat "\n; ERROR : There is no module file - "
$_filename
" ."
)
)
(exit)
)
)
)
(defun include:check-circular-reference:dump-string (stack indent)
(if stack
(strcat "\n"
(include:make-list (* indent 2) 32 ;|(ascii " ")|;)
"-->"
(vl-princ-to-string (car stack))
(include:check-circular-reference:dump-string
(cdr stack)
(1+ indent)
)
)
""
)
)
(defun include:check-circular-reference ($_func $_symbol-info $_load-list-symbol /
$_result
)
(if
(null
(vl-member-if
(function
(lambda (symbol-info)
(= (strcase (cdr symbol-info))
(strcase (cdr $_symbol-info))
)
)
)
*include:loading-stack*
)
)
(progn
(setq *include:loading-stack* (cons $_symbol-info
*include:loading-stack*
)
)
(if
(vl-catch-all-error-p
(setq $_result (vl-catch-all-apply
$_func
(list (cdr $_symbol-info) $_load-list-symbol)
)
)
)
(progn (setq *include:loading-stack* nil) (exit))
(progn
(setq *include:loading-stack* (cdr *include:loading-stack*))
$_result
)
)
)
(include:message
0
(strcat "\n; NOTICE : "
(cdr $_symbol-info)
" of "
(vl-symbol-name (car $_symbol-info))
" is causing a circular reference!\n; STACK is -> "
(include:check-circular-reference:dump-string
(recerce (cons $_symbol-info *include:loading-stack*))
0
)
)
)
)
)
;;;*******************************************************************
;;;
;;; include function
;;;
;;;*******************************************************************
(defun include:symbol-info (asymbol filename / symbol-info)
(if (setq symbol-info (assoc asymbol *include:symbol-table*))
(if (/= (strcase (cdr symbol-info)) (strcase filename))
(progn
(include:message
10
(strcat "\n; CAUTION : "
(vl-symbol-name asymbol)
" is doubly defined in "
filename
" and "
(cdr symbol-info)
" ."
)
)
(exit)
)
)
)
(cons asymbol filename)
)
(defun include ($_include:symbol $_include:filename / $_symbol-info $_list-item
$_result
)
(if
(and (not (boundp $_include:symbol))
(setq $_symbol-info ;_ Check the double definition.
(include:symbol-info
$_include:symbol
(include:replace-to-slash $_include:filename)
)
)
)
(progn
(setq $_result (include:check-circular-reference
'include:load-file
$_symbol-info
'*include:LibraryFiles*
)
*include:symbol-table* (if
(setq $_list-item (assoc $_include:symbol
*include:symbol-table*
)
)
(subst $_symbol-info
$_list-item
*include:symbol-table*
)
(cons $_symbol-info
*include:symbol-table*
)
)
)
(if (not (boundp $_include:symbol))
(include:message
10
(strcat "\n; CAUTION : "
(vl-symbol-name $_include:symbol)
" is not defined in "
$_include:filename
)
)
)
$_result
)
)
)
;;;*******************************************************************
;;;
;;; load-file function
;;;
;;;*******************************************************************
(defun load-file ($_load-file:filename / $_result)
(include:check-circular-reference
'include:load-file
(cons
(read
(strcat "$_load-file::"
(vl-filename-base $_load-file:filename)
)
)
$_load-file:filename
)
'*include:LoadFiles*
)
)
;;;*******************************************************************
;;;
;;; resource-file function
;;;
;;;*******************************************************************
(defun resource-file:main (filename / file-info list-item)
(if
(setq file-info ;_ Check for file availability
(include:file-info filename nil '*include:ResourceFiles*)
)
(setq *include:ResourceFiles* (if
(setq list-item (assoc (strcase filename)
*include:ResourceFiles*
)
)
(subst file-info
list-item
*include:ResourceFiles*
)
(cons file-info *include:ResourceFiles*)
)
)
(progn
(princ
(strcat "\n; ERROR : There is no resource file - "
filename
" ."
)
)
(exit)
)
)
)
(defun resource-file (filename / ext)
(if (/= (setq ext (vl-filename-extension filename)) "")
(if (member (read (substr ext 2)) '(DCL PRJ TXT VLX FAS LSP))
(resource-file:main filename)
(progn (princ "\n; ERROR : Illegal file name extension. (DCL TXT)")
(exit)
)
)
(progn
(princ
"\n; ERROR : The file name must have an extension. (DCL TXT)"
)
(exit)
)
)
)
;;;*******************************************************************
;;;
;;; createPRV function
;;;
;;;*******************************************************************
(defun createPRV:write:sub (file-info-list)
(if file-info-list
(progn
(princ
(strcat "\n (:"
(strcase (vl-symbol-name (cadr (car file-info-list))) T)
" "
(vl-prin1-to-string (caddr (car file-info-list)))
")"
)
fp
)
(createPRV:write:sub (cdr file-info-list))
)
)
)
(defun createPRV:write (fp target obj-temp)
(princ ";;; Visual LISP Apprication make file" fp)
(princ "\n(PRV-DEF" fp)
(princ (strcat "\n (:target . " (vl-prin1-to-string target) ")")
fp
)
(princ "\n (:active-x . T)" fp)
(princ "\n (:separate-namespace . T)" fp)
(princ "\n (:protected . T)" fp)
(princ "\n (:load-file-list" fp)
(createPRV:write:sub (reverse *include:LoadFiles*))
;;
(princ "\n )" fp)
(princ "\n (:require-file-list" fp)
(createPRV:write:sub (reverse *include:LibraryFiles*))
(createPRV:write:sub
(vl-sort *include:ResourceFiles*
(function
(lambda (file-info-1 file-info-2)
(< (vl-position (cadr file-info-1) resource-order)
(vl-position (cadr file-info-2) resource-order)
)
)
)
)
)
;;
(princ "\n )" fp)
(princ
(strcat "\n (:ob-directory . "
(vl-prin1-to-string obj-temp)
")"
)
fp
)
(princ "\n (:tmp-directory)" fp)
(princ "\n (:optimization . st)" fp)
(princ "\n)" fp)
(princ "\n;;; EOF" fp)
)
(defun createPRV (obj-temp target-folder / resource-order filename fp)
(setq resource-order '(DCL PRJ TXT VLX FAS LSP))
;;
(if
(setq filename (getfiled "Create Visual LISP PRV file"
(if (null *include:working-folder*)
(getvar "MYDOCUMENTSPREFIX")
*include:working-folder*
)
"prv"
(+ 1 4 16 128)
)
)
(if (setq fp (open filename "w"))
(progn
(if
(vl-catch-all-error-p
(vl-catch-all-apply
'createPRV:write
(list fp
(strcat
(include:normalize-path
(if (null target-folder)
""
(include:replace-to-slash target-folder)
)
T
)
(vl-filename-base filename)
".VLX"
)
(if (null obj-temp)
"temp"
(include:normalize-path
(include:replace-to-slash obj-temp)
nil
)
)
)
)
)
(princ "\n; An error occurred while writing the file.")
(progn (princ "\n; An application make file has been created.")
(princ (strcat "\n; " (include:replace-to-slash filename)))
(if
(not
(or *include:LoadFiles*
*include:LibraryFiles*
*include:ResourceFiles*
)
)
(princ "\n;\n; But the list of files was empty.\n;")
)
(setq *include:working-folder* (vl-filename-directory filename))
)
)
(close fp)
)
(princ "\n; A file open error has occurred.")
)
)
(princ)
)
;;;********************************************************************
;;; Below functions are used with only Visual LISP ********************
;;;********************************************************************
(if (or (null (getvar "LISPSYS")) (zerop (getvar "LISPSYS")))
(progn
;;*******************************************************************
;;
;; exportLib function
;;
;;*******************************************************************
(defun exportLib:write:sub (file-info-list)
(if file-info-list
(progn
(princ
(strcat "\n "
(vl-prin1-to-string (caddr (car file-info-list)))
)
fp
)
(exportLib:write:sub (cdr file-info-list))
)
)
)
(defun exportLib:write (fp filename fas-temp temp single / fp)
(princ ";;; Visual LISP project file" fp)
(princ "\n(VLISP-PROJECT-LIST" fp)
(princ "\n :NAME" fp)
(princ (strcat "\n " filename) fp)
(princ "\n :OWN-LIST" fp)
(princ "\n (" fp)
(exportLib:write:sub (reverse *include:LibraryFiles*))
(princ "\n )" fp)
(princ "\n :FAS-DIRECTORY" fp)
(princ (strcat "\n " (vl-prin1-to-string fas-temp)) fp)
(princ "\n :TMP-DIRECTORY" fp)
(princ (strcat "\n " (vl-prin1-to-string temp)) fp)
(princ "\n :PROJECT-KEYS" fp)
(princ
(strcat "\n (:BUILD (:standard) :MERGED "
(if single
"T"
"nil"
)
" :SAFE-MODE T :MSGLEVEL 1)"
)
fp
)
(princ "\n :CONTEXT-ID" fp)
(princ "\n :AUTOLISP" fp)
(princ "\n)" fp)
(princ "\n;;; EOF" fp)
)
(defun exportLib (fas-temp temp / filename fp)
(if
(setq filename (getfiled "Export Libraries to Visual LISP Project file"
(if (null *include:working-folder*)
(getvar "MYDOCUMENTSPREFIX")
*include:working-folder*
)
"prj"
(+ 1 4 16 128)
)
)
(if (setq fp (open filename "w"))
(progn
(if
(vl-catch-all-error-p
(vl-catch-all-apply
'exportLib:write
(list fp
(vl-filename-base filename)
(if (null fas-temp)
"temp"
(include:normalize-path
(include:replace-to-slash fas-temp)
nil
)
)
(if (null temp)
"temp"
(include:normalize-path
(include:replace-to-slash temp)
nil
)
)
nil
)
)
)
(princ "\n; An error occurred while writing the file.")
(progn
(princ
"\n; The list of library files was written to the project file"
)
(princ (strcat "\n; " (include:replace-to-slash filename)))
(if (null *include:LibraryFiles*)
(princ "\n;\n; But the list of files was empty.\n;")
)
(setq *include:working-folder* (vl-filename-directory filename))
)
)
(close fp)
)
(princ "\n; A file open error has occurred.")
)
)
(princ)
)
;;*******************************************************************
;;
;; exportResource function
;;
;;*******************************************************************
(defun exportResource () (exportLib nil nil))
) ;_ end-of-progn
) ;_ end-of-if
;;;
(princ "\ninclude-lib was loaded.")
(princ)