-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.lisp
1443 lines (1216 loc) · 56.4 KB
/
common.lisp
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;; -*- mode:lisp; coding:utf-8; -*-
;;;;****************************************************************************
;;;;FILE: common.lisp
;;;;LANGUAGE: Common-Lisp
;;;;SYSTEM: None
;;;;USER-INTERFACE: None
;;;;DESCRIPTION
;;;;
;;;; This file contains initializations common to clisp, cmucl, sbcl, and
;;;; possibly to other Common-Lisp too.
;;;;
;;;;AUTHORS
;;;; <PJB> Pascal Bourguignon
;;;;MODIFICATIONS
;;;; 2006-08-28 <PJB> Moved interactive functions
;;;; to COM.INFORMATIMAGO.COMMON-LISP.INTERACTIVE.
;;;; This file should contains only environment
;;;; configuration forms (asdf, logical pathnames, etc).
;;;; 2004-11-23 <PJB> Added LIST-ALL-SYMBOLS and LIST-EXTERNAL-SYMBOLS.
;;;; 2003-05-02 <PJB> Created.
;;;;BUGS
;;;;LEGAL
;;;; GPL
;;;;
;;;; Copyright Pascal Bourguignon 2003 - 2015
;;;; mailto:pjb@informatimago.com
;;;;
;;;; This program is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU General Public License
;;;; as published by the Free Software Foundation; either version
;;;; 2 of the License, or (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be
;;;; useful, but WITHOUT ANY WARRANTY; without even the implied
;;;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;;;; PURPOSE. See the GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public
;;;; License along with this program; if not, write to the Free
;;;; Software Foundation, Inc., 59 Temple Place, Suite 330,
;;;; Boston, MA 02111-1307 USA
;;;;****************************************************************************
(in-package "COMMON-LISP-USER")
;;;----------------------------------------------------------------------
;;;
;;; Clean the packages imported into COMMON-LISP-USER:
;;;
(mapc (lambda (package) (unuse-package package "COMMON-LISP-USER"))
(set-difference
(copy-seq (package-use-list "COMMON-LISP-USER"))
(delete nil (list ;; A list of all the "CL" packages possible:
(find-package "COMMON-LISP")
(find-package "IMAGE-BASED-COMMON-LISP")))))
(defun reset-cl-user ()
(let ((myself 'reset-cl-user))
(setf *package* (find-package "KEYWORD"))
(delete-package "COMMON-LISP-USER")
(setf *package* (make-package "COMMON-LISP-USER"
:nicknames '("CL-USER")
:use '("COMMON-LISP"
;; And perhaps some other custom packages.
)))
;; You may also import some specific symbols:
#+ccl (import 'ccl:quit)
(import myself))
*package*)
(setf *print-circle* t
*print-length* nil
*print-level* nil
*print-lines* nil
*print-right-margin* 110)
#+swank
(setq *debugger-hook* (let ((old-debugger-hook *debugger-hook*))
(defun trace-error-debugger-hook (condition me-or-my-encapsulation)
(format *trace-output* "~A~%" condition)
(funcall old-debugger-hook condition me-or-my-encapsulation))))
;;;----------------------------------------------------------------------
;;;
;;; COM.INFORMATIMAGO.PJB
;;;
(defpackage "COM.INFORMATIMAGO.PJB.UTILITY"
(:use "COMMON-LISP")
(:shadow "USER-HOMEDIR-PATHNAME" "MAKE-PATHNAME" "TRANSLATE-LOGICAL-PATHNAME")
(:export "USER-HOMEDIR-PATHNAME" "MAKE-PATHNAME" "TRANSLATE-LOGICAL-PATHNAME"
"LOAD-ASDF3" "ASDF-CONFIGURATION" "LOAD-QUICKLISP" "INFORMATIMAGO-PACKAGES"
"*ORIGINAL-READTABLE*")
(:documentation "
Utilities for the com.informatimago.pjb package.
License:
AGPL3
Copyright Pascal J. Bourguignon 2003 - 2015
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program.
If not, see <a href=\"http://www.gnu.org/licenses/\">http://www.gnu.org/licenses/</a>.
"))
(defpackage "COM.INFORMATIMAGO.PJB"
(:nicknames "PJB")
(:use "COMMON-LISP"
"COM.INFORMATIMAGO.PJB.UTILITY")
(:shadow "ED" "REP") ; from com.informatimago.common-lisp.cesarum.ecma048
(:shadow "APROPOS" "APROPOS-LIST") ; from com.informatimago.tools.symbol
(:shadowing-import-from "COM.INFORMATIMAGO.PJB.UTILITY"
"USER-HOMEDIR-PATHNAME" "MAKE-PATHNAME"
"TRANSLATE-LOGICAL-PATHNAME")
(:export "RESET-OPTIMIZATION"
"LIST-DIRECTORIES" "GET-DIRECTORY"
"LIST-LOGICAL-HOSTS" "DEFINE-LOGICAL-HOST-TRANSLATIONS"
"START-DRIBBLE"
"DEFINE-AUTOLOAD"
"HOSTNAME"
"SCHEME"
"*INP*" "*OUT*"
"SELF"
"IT" "THIS" "THAT"
"THEM" "THESE" "THOSE"
"IS" "WAS" "WERE"
"DEFINE-COMMAND" "HELP" "IMPORT-COMMANDS"
"QUIT" "EXIT"
"ENABLE-SHELL-ESCAPE"
"DISABLE-SHELL-ESCAPE")
(:documentation "
This package contains REPL utilities, defined in ~/rc/common.lisp,
which is loaded from the various rc files of the various CL
implementations.
It also re-exports the exported symbols of
most COM.INFORMATIMAGO.* packages, but a few.
License:
AGPL3
Copyright Pascal J. Bourguignon 2003 - 2015
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program.
If not, see <a href=\"http://www.gnu.org/licenses/\">http://www.gnu.org/licenses/</a>.
"))
(in-package "COM.INFORMATIMAGO.PJB")
(defun reset-optimization ()
(proclaim '(optimize (safety 3) (debug 3) (space 0) (speed 0) (compilation-speed 3))))
(reset-optimization)
;;;----------------------------------------------------------------------
;;;
(in-package "COM.INFORMATIMAGO.PJB.UTILITY")
(defvar *original-readtable* (copy-readtable *readtable*))
;;;----------------------------------------------------------------------
;;;
;;; COM.INFORMATIMAGO.TOOLS.PATHNAME
;;;
(defun user-homedir-pathname ()
"On MS-Windows, it's not the USER-HOMEDIR-PATHNAME."
#+windows-target (let ((home (ccl::getenv "HOME")))
(if home
(pathname (format nil "~A\\" home))
#P"C:\\cygwin\\home\\pjb\\"))
#-windows-target (cl:user-homedir-pathname))
(defun make-pathname (&key (host nil hostp) (device nil devicep) (directory nil directoryp)
(name nil namep) (type nil typep) (version nil versionp)
(defaults nil defaultsp) (case :local casep))
(declare (ignorable casep))
#+(or abcl ccl allegro)
(labels ((localize (object)
(typecase object
(list (mapcar (function localize) object))
(string (string-downcase object))
(t object)))
(parameter (indicator key value)
(when indicator
(list key (if (eql case :common)
(localize value)
value)))))
(apply (function cl:make-pathname)
(append (parameter hostp :host host)
(parameter devicep :device device)
(parameter directoryp :directory directory)
(parameter namep :name name)
(parameter typep :type type)
(parameter versionp :version version)
(parameter defaultsp :defaults defaults)
(list :case :local))))
#-(or abcl ccl allegro)
(apply (function cl:make-pathname)
(append
(when hostp (list :host host))
(when devicep (list :device device))
(when directoryp (list :directory directory))
(when namep (list :name name))
(when typep (list :type type))
(when versionp (list :version version))
(when defaultsp (list :defaults defaults))
(when casep (list :case case)))))
;;;----------------------------------------------------------------------
;;;
;;; ASDF-CONFIGURATION
;;;
;; (setf asdf:*compile-file-failure-behaviour* :ignore)
(defun load-asdf3 ()
(unless (member :asdf3 *features*)
(or (ignore-errors (load (merge-pathnames #P"quicklisp/asdf.lisp"
(user-homedir-pathname))))
(load (merge-pathnames #P"src/public/lisp/tools/asdf.lisp"
(user-homedir-pathname))))))
(defun asdf-configuration ()
"Creates the ~/.config/common-lisp/asdf-output-translations.conf file."
(let ((asdf-conf-path (merge-pathnames
(make-pathname :directory '(:relative ".config" "common-lisp")
:name "asdf-output-translations"
:type "conf"
:case :local
:defaults (user-homedir-pathname))
(user-homedir-pathname) nil))
(common-path *load-truename*))
(when (or (not (ignore-errors (probe-file asdf-conf-path)))
(null (file-write-date asdf-conf-path))
(and common-path
(or (null (file-write-date common-path))
(< (file-write-date asdf-conf-path)
(file-write-date common-path)))))
(ensure-directories-exist asdf-conf-path)
(with-open-file (asdfconf asdf-conf-path
:direction :output
:if-does-not-exist :create
:if-exists nil
:external-format :default)
(write-string ";; -*- mode:lisp -*-" asdfconf)
(print '(:OUTPUT-TRANSLATIONS
:ignore-invalid-entries
;; :ignore-inherited-configuration
;; TODO: Replace :hostname which doesn't seem to be supported anymore by a function.
(T (:HOME ".cache" "common-lisp" :HOSTNAME :IMPLEMENTATION))
(T (:HOME ".cache" "common-lisp" :IMPLEMENTATION))
:INHERIT-CONFIGURATION)
asdfconf)
(terpri asdfconf)))))
;;;----------------------------------------------------------------------
;;;
;;; QuickLisp
;;;
(defun load-quicklisp (&key (if-does-not-exist :error))
"Loads quicklisp."
(let ((quicklisp (merge-pathnames
(make-pathname :directory '(:relative "QUICKLISP")
:name "SETUP"
:type "LISP"
:version :newest
:case :common
:defaults (user-homedir-pathname))
(user-homedir-pathname)
nil)))
(if (probe-file quicklisp)
(load quicklisp)
(case if-does-not-exist
((:error)
(error "Please install quicklisp. I expect it in ~S" quicklisp))
(otherwise
if-does-not-exist)))))
;;;----------------------------------------------------------------------
;;;
(defun informatimago-packages ()
"Returns a list of interesting informatimago packages."
(remove-if
(lambda (package)
(flet ((prefixp (string prefix)
(and (<= (length prefix) (length string))
(string= prefix string :end2 (length prefix)))))
(let ((name (package-name package)))
(or (not (or (prefixp name "COM.INFORMATIMAGO.COMMON-LISP.")
(member name '("COM.INFORMATIMAGO.TOOLS.QUICKLISP"
"COM.INFORMATIMAGO.TOOLS.ASDF"
"COM.INFORMATIMAGO.TOOLS.THREAD"
"COM.INFORMATIMAGO.TOOLS.SYMBOL")
:test (function string=))))
(member name '("COM.INFORMATIMAGO.COMMON-LISP.PARSER."
"COM.INFORMATIMAGO.COMMON-LISP.HTML-GENERATOR."
"COM.INFORMATIMAGO.COMMON-LISP.LISP-READER."
"COM.INFORMATIMAGO.COMMON-LISP.LISP.STEPPER"
"COM.INFORMATIMAGO.COMMON-LISP.HEAP"
"COM.INFORMATIMAGO.COMMON-LISP.REGEXP.REGEXP-")
:test (function prefixp))
(member name '("COM.INFORMATIMAGO.COMMON-LISP.CESARUM.JULIAN-CALENDAR"
"COM.INFORMATIMAGO.COMMON-LISP.CESARUM.GREGORIAN-CALENDAR"
"COM.INFORMATIMAGO.COMMON-LISP.CESARUM.DATE"
"COM.INFORMATIMAGO.COMMON-LISP.CESARUM.SET"
"COM.INFORMATIMAGO.COMMON-LISP.CESARUM.INDEX-SET"
"COM.INFORMATIMAGO.COMMON-LISP.CESARUM.BSET"
"COM.INFORMATIMAGO.COMMON-LISP.CESARUM.BRELATION"
"COM.INFORMATIMAGO.COMMON-LISP.CESARUM.GRAPH"
"COM.INFORMATIMAGO.COMMON-LISP.CESARUM.MERSENNE"
"COM.INFORMATIMAGO.COMMON-LISP.CESARUM.XOROSHIRO128"
"COM.INFORMATIMAGO.COMMON-LISP.DATA.CONSTANT"
"COM.INFORMATIMAGO.COMMON-LISP.UNIX.OPTION"
"COM.INFORMATIMAGO.COMMON-LISP.ED.ED"
"COM.INFORMATIMAGO.COMMON-LISP.CESARUM.ECMA048")
:test (function string=))))))
(list-all-packages)))
;;;
;;;----------------------------------------------------------------------
(in-package "COM.INFORMATIMAGO.PJB")
(defparameter *ql-present* (load-quicklisp :if-does-not-exist nil))
(load-asdf3)
(asdf-configuration)
(unless *ql-present*
(defpackage "QUICKLISP"
(:use)
(:nicknames "QL" "QL-CONFIG")
(:export "*LOCAL-PROJECT-DIRECTORIES*"
"QUICKLOAD"
"CONFIG-VALUE")))
;; (defvar ql:*local-project-directories* (directory #P "~/quicklisp/local-projects/**/"))
;; (when *ql-present*
;; (setf (ql-config:config-value "proxy-url")
;; (with-open-file (proxy-url #P"~/.proxy") (read-line proxy-url))))
(defun load-one-way-or-another (system &key (verbose t))
(if *ql-present*
(ql:quickload system :verbose verbose)
(asdf:oos 'asdf:load-op system :verbose verbose)))
#-(and)
(defun find-directories-with-asd-files (root-pathname)
;; going around a bug in clisp where (directory "…/../…") takes too long.
(loop
:with old-set := '()
:for dir := (make-pathname :name nil :type nil :version nil :defaults root-pathname)
:then (make-pathname :directory (append (pathname-directory dir) '(:wild))
:name :wild
:type "asd"
:case :local
:defaults dir)
:do (let ((new-set (sort (append old-set (directory dir))
(lambda (a b) (string< (namestring a) (namestring b))))))
(if (= (length new-set) (length old-set))
(return-from find-directories-with-asd-files new-set)
(setf old-set new-set)))))
(defun find-directories-with-asd-files (root-pathname)
(sort (delete-duplicates
(mapcar (lambda (path)
(make-pathname :name nil :type nil :version nil :defaults path))
(directory (merge-pathnames "**/*.asd" root-pathname nil)))
:test (function equal))
(function string>=)
:key (function namestring)))
(defun register-system-location (directory-pathname &key (force nil))
(let ((truename (ignore-errors (truename directory-pathname))))
(if truename
(let* ((stem (format nil "~8,'0X" (sxhash (namestring (truename directory-pathname)))))
(cached (merge-pathnames (make-pathname :directory '(:relative ".cache" "common-lisp" "rc")
:name stem :type "sexp" :version nil)
(user-homedir-pathname))))
(flet ((scan-and-cache-directories ()
(format t "~&;; Scanning for .asd files directory ~S~%"
directory-pathname)
(force-output)
(ensure-directories-exist cached)
(with-open-file (out cached :direction :output :if-does-not-exist :create :if-exists :supersede)
(let ((dirs (find-directories-with-asd-files directory-pathname)))
(prin1 dirs out)
(terpri out)
dirs))))
(let ((dirs (if force
(scan-and-cache-directories)
(or (with-open-file (inp cached :direction :input :if-does-not-exist nil)
(when inp
(read inp)))
(scan-and-cache-directories)))))
;; forget quicklisp, symbolic links are ignored explicitely from local-projects.
;; (push directory-pathname ql:*local-project-directories*)
;; (ql:register-local-projects)
(setf asdf:*central-registry* (append asdf:*central-registry* dirs)))))
(warn "~A does not exist" directory-pathname))))
;; #+#.(cl:if *ql-present* '(:and) '(:or))
;; (progn
(load-one-way-or-another "uiop")
(register-system-location #P"~/src/public/lisp/")
(load-one-way-or-another "com.informatimago.common-lisp" :verbose nil)
(load-one-way-or-another "com.informatimago.common-lisp.lisp.stepper" :verbose nil)
(load-one-way-or-another "com.informatimago.clmisc" :verbose nil)
(load-one-way-or-another "com.informatimago.common-lisp.lisp-sexp" :verbose nil)
;; (load-one-way-or-another "com.informatimago.tools")
(load-one-way-or-another '("com.informatimago.tools.pathname"
"com.informatimago.tools.manifest"
"com.informatimago.tools.symbol"
"com.informatimago.tools.source"
"com.informatimago.tools.reader-macro"
"com.informatimago.tools.summary"
"com.informatimago.tools.thread"
"com.informatimago.tools.quicklisp"
"com.informatimago.tools.make-depends"
"com.informatimago.tools.script"
"com.informatimago.tools.check-asdf")
:verbose nil)
#-(or ccl cmu ecl sbcl)
(load-one-way-or-another "com.informatimago.clext")
#+clisp
(load-one-way-or-another "com.informatimago.clisp")
#+disabled-temporarily-for-bitrot
(load-one-way-or-another "com.informatimago.susv3")
(load-one-way-or-another "alexandria" :verbose nil)
(defun informatimago-import-export ()
;; I can't find where those symbols are interned, but they collide with used packages…
(mapc (function unintern) '(version version< version<=
split-string))
(shadowing-import '(com.informatimago.tools.pathname:make-pathname
com.informatimago.tools.pathname:translate-logical-pathname
com.informatimago.tools.pathname:user-homedir-pathname))
(dolist (package (informatimago-packages))
(format *trace-output* "~&;; Using package ~A~%" (package-name package))
(handler-case (use-package package)
(error (err)
(invoke-debugger err)
(princ err) (terpri))))
(shadowing-import '(com.informatimago.common-lisp.interactive.interactive:rep))
(shadowing-import '(com.informatimago.tools.symbol:apropos
com.informatimago.tools.symbol:apropos-list))
(dolist (package (informatimago-packages))
(export (com.informatimago.common-lisp.cesarum.package:list-external-symbols package))))
(informatimago-import-export)
(initialize)
;;;----------------------------------------------------------------------
;;;
(eval-when (:compile-toplevel :load-toplevel :execute)
(flet ((fundefine (fname)
(when (fboundp fname)
(fmakunbound fname))))
(fundefine 'get-directory)
(fundefine 'make-translations)))
;;;----------------------------------------------------------------------
;;;
;;; Directories.
;;;
(defvar *default-directories*
'(:asdf-install "/usr/local/share/lisp/asdf-install/"
:share-lisp "/usr/local/share/lisp/"
:lisp-sources "/home/pjb/src/public/lisp/"
:pjb-lisp "/home/pjb/src/lisp/"))
(defvar *directories* '())
(defun list-directories ()
"Returns the list of named directories."
(copy-seq *directories*))
(defun get-directory (key &optional (subpath ""))
"
Caches the ~/directories.txt file that contains a map of
directory keys to pathnames, into *DIRECTORIES*.
Then builds and returns a pathname made by merging the directory
selected by KEY, and the given SUBPATH.
"
(unless *directories*
(with-open-file (dirs (merge-pathnames
(make-pathname :name "DIRECTORIES" :type "TXT"
:version nil :case :common)
(user-homedir-pathname)
nil)
:if-does-not-exist nil)
(if dirs
(handler-case
(loop
:for k = (read dirs nil dirs)
:until (eq k dirs)
:do (push (string-trim " " (read-line dirs)) *directories*)
:do (push (intern (substitute #\- #\_ (string k)) "KEYWORD") *directories*))
(error (err)
(warn "Error while reading directories.txt file: ~A" err)))
(progn
(warn "No directories.txt file.")
(setf *directories* *default-directories*)))))
(unless (getf *directories* key)
(error "~S: No directory keyed ~S" 'get-directory key))
(merge-pathnames subpath (getf *directories* key) nil))
;;;----------------------------------------------------------------------
;;;
;;; Logical hosts -- the Common-Lisp way to paths.
;;;
(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar *logical-hosts* '()
"A list of logical hosts defined by DEFINE-LOGICAL-PATHNAME-TRANSLATIONS.
COMMON-LISP doesn't provide any introspection function for logical hosts."))
(defun list-logical-hosts ()
"Return a list of logical hosts defined by DEFINE-LOGICAL-PATHNAME-TRANSLATIONS."
(copy-list *logical-hosts*))
(defun make-translations (host logical-dir physical-dir &optional file-type)
"
Returns logical pathname translations for the given HOST, mapping the
logical directory LOGICAL-DIR and all the files with the given
FILE-TYPE, in its subdirectories, to the physical directory
PHYSICAL-DIR.
If no FILE-TYPE is given, or if it's NIL, then a wildcard is used for
the file type, and the logical pathnames are translated with and
without this wildcard, in an orden that's implementation dependant.
The inclusion of a version wildcard is also implementation dependant.
"
(mapcar
(lambda (item)
(destructuring-bind (logical-tail physical-tail) item
(list (apply (function make-pathname)
:host host
:directory `(:absolute ,@logical-dir :wild-inferiors)
:case :common
logical-tail)
(format nil "~A**/~A" physical-dir physical-tail))))
#+clisp
(if file-type
`(((:name :wild :type ,file-type :version nil) ,(format nil "*.~(~A~)" file-type)))
'(((:name :wild :type :wild :version nil) "*.*")
((:name :wild :type nil :version nil) "*")))
#+sbcl
(if file-type
`(((:name :wild :type ,file-type :version :wild) ,(format nil "*.~(~A~)" file-type)))
'(((:name :wild :type :wild :version :wild) "*.*")))
#+(or allegro ccl)
(if file-type
`(((:name :wild :type ,file-type :version nil) ,(format nil "*.~(~A~)" file-type)))
'(((:name :wild :type nil :version nil) "*")
((:name :wild :type :wild :version nil) "*.*")))
#-(or clisp sbcl allegro ccl)
(if file-type
`(((:name :wild :type ,file-type :version nil) ,(format nil "*.~(~A~)" file-type))
((:name :wild :type ,file-type :version :wild) ,(format nil "*.~(~A~)" file-type)))
'(((:name :wild :type nil :version nil) "*")
((:name :wild :type :wild :version nil) "*.*")
((:name :wild :type :wild :version :wild) "*.*")))))
(defun define-logical-pathname-translations (host path &optional (subpath ""))
"
Defines a new logical pathname (or overrides an existing one)
for the given HOST, unless the logical HOST already exists.
PATH and SUBPATH must be physical pathnames and subpaths. The logical
pathname translation maps logical pathnames directly from the root of
the HOST, to the given path and subpath concatenated.
The HOST is added to the list of logical hosts defined.
"
(pushnew host *logical-hosts* :test (function string-equal))
;; If the HOST is already defined we don't change it (eg. HOME):
(unless (handler-case (logical-pathname-translations host) (error () nil))
(and (ignore-errors (setf (logical-pathname-translations host) nil) t)
(setf (logical-pathname-translations host)
(make-translations
host '() (format nil "~A~:[~;~:*~A~]"
path (if (string= "" subpath) nil subpath)))))))
;;;------------------------------------------------------------------------
(defparameter *asdf-install-location* (get-directory :asdf-install))
(defparameter *share-lisp* (get-directory :share-lisp))
(defparameter *pjb-comm* (get-directory :lisp-sources))
(defparameter *pjb-lisp* (get-directory :pjb-lisp))
(define-logical-pathname-translations "SHARE-LISP" *share-lisp*)
(define-logical-pathname-translations "PJB-COMM" *pjb-comm*)
(define-logical-pathname-translations "PJB-LISP" *pjb-lisp*)
(define-logical-pathname-translations "CMU-AI" *share-lisp* "ai/")
(define-logical-pathname-translations "CL-PDF" *share-lisp* "cl-pdf/")
(define-logical-pathname-translations "UFFI" *share-lisp* "uffi/")
(define-logical-pathname-translations "PACKAGES" *share-lisp* "packages/")
(define-logical-pathname-translations "CLOCC" *share-lisp* "packages/net/sourceforge/clocc/clocc/")
(define-logical-pathname-translations "CCLAN" *share-lisp* "packages/net/sourceforge/cclan/")
(define-logical-pathname-translations "DEFSYSTEM" *share-lisp*
;; We must go thru a translation for defsystem-3.x isn't a valid logical name!
"packages/net/sourceforge/clocc/clocc/src/defsystem-3.x/")
#-cmu
(define-logical-pathname-translations "HOME" (user-homedir-pathname) "")
(define-logical-pathname-translations "LOADERS" *pjb-comm* "cl-loaders/")
;;(DEFINE-LOGICAL-PATHNAME-TRANSLATIONS "NORVIG" *PJB-LISP* "norvig/")
(define-logical-pathname-translations "NORVIG" #p"/home/pjb/src/lisp/ai/" "norvig-paip-pjb/")
(define-logical-pathname-translations "LOGHOSTS" (user-homedir-pathname) "LOGHOSTS/")
;;;------------------------------------------------------------------------
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun post-process-logical-host-translations ()
(when (fboundp 'common-lisp-user::post-process-logical-pathname-translations)
(map nil
'common-lisp-user::post-process-logical-pathname-translations
*logical-hosts*))))
(post-process-logical-host-translations)
;;;----------------------------------------------------------------------
(defmacro define-autoload (name arguments loader)
"Defines a function that will load the LOADER file, before calling itself again."
`(progn
(declaim (notinline ,name))
(defun ,name ,arguments
"Autoload function."
(load ,loader)
(,name ,@arguments))))
#-ccl
(define-autoload scheme () (translate-logical-pathname #P"LOADERS:PSEUDO"))
;;;----------------------------------------------------------------------
(defun start-dribble ()
"We dribble to a timestamped file in a specific #P\"HOME:DRIBBLE;\" directory."
(let ((path
(merge-pathnames
(make-pathname
:directory '(:relative "DRIBBLES")
:name (flet ((implementation-id ()
(flet ((first-word (text)
(let ((pos (position (character " ") text)))
(remove (character ".")
(if pos (subseq text 0 pos) text)))))
(format
nil
"~A-~A-~A"
(cond
((string-equal
"International Allegro CL Enterprise Edition"
(lisp-implementation-type))
"ACL")
(t (first-word (lisp-implementation-type))))
(first-word (lisp-implementation-version))
(first-word (machine-type))))))
(multiple-value-bind (se mi ho da mo ye)
(decode-universal-time (get-universal-time))
(format nil "~4,'0D~2,'0D~2,'0DT~2,'0D~2,'0D~2,'0D-~A"
ye mo da ho mi se (implementation-id))))
:type "DRIBBLE"
:version nil
:defaults (user-homedir-pathname))
(user-homedir-pathname) nil)))
(ensure-directories-exist path)
(dribble path)))
;; (start-dribble)
;;;----------------------------------------------------------------------
(defun executable-reader (stream ch subch)
(declare (ignorable ch subch))
#+clisp (sys::unix-executable-reader stream ch subch)
#-clisp (progn
(read-line stream)
(values)))
(set-dispatch-macro-character #\# #\! (function executable-reader))
(defvar *shell-command-status* 0)
(defun shell-escape (stream ch)
(declare (ignore ch))
`(progn
(setf *shell-command-status*
(nth-value 2
(uiop:run-program
,(with-output-to-string (out)
(write-string "source ~/.bashrc ; ( " out)
(loop
:for line := (read-line stream nil nil)
:while (and line (char= #\\ (aref line (1- (length line)))))
:do (write-line line out)
:finally (write-string line out))
(write-string " ) | expand -8" out))
:input nil
:output t
:error-output t
:ignore-error-status t
:force-shell t)))
(values)))
(defmacro enable-shell-escape ()
`(eval-when (:compile-toplevel :load-toplevel :execute)
(set-macro-character #\! 'shell-escape)))
(defmacro disable-shell-escape ()
`(eval-when (:compile-toplevel :load-toplevel :execute)
(set-macro-character #\! nil)
(set-syntax-from-char #\! #\a)))
;;;----------------------------------------------------------------------
(defun shell (control-string &rest arguments)
(uiop:run-program (format nil "~?" control-string arguments)
:force-shell t :output t))
(setf com.informatimago.common-lisp.interactive.browser:*shell*
(lambda (command) (shell "~A" command)))
(defun shell-command-to-string (control-string &rest arguments)
(uiop:run-program (format nil "~?" control-string arguments)
:force-shell t :output :string))
(export '(shell shell-command-to-string) :com.informatimago.pjb)
;; (progn
;; (apply (function asdf:run-shell-command) control-string arguments) is deprecated.
;; #-ccl (error "~S is not implemented yet on ~A" 'shell (lisp-implementation-type))
;; #+ccl
;; (let ((process
;; (ccl:run-program "/bin/bash"
;; (list "-c" (format nil "~?" control-string arguments))
;; :output :stream
;; :error :stream)))
;; (com.informatimago.common-lisp.cesarum.stream:copy-stream
;; (ccl:external-process-output-stream process)
;; *standard-output*)
;; (com.informatimago.common-lisp.cesarum.stream:copy-stream
;; (ccl:external-process-error-stream process)
;; *error-output*))
;;
;; #-ccl (error "~S is not implemented yet on ~A" 'shell-command-to-string (lisp-implementation-type))
;; #+ccl
;; (let ((process
;; (ccl:run-program "/bin/bash"
;; (list "-c" (format nil "~?" control-string arguments))
;; :output :stream
;; :error :stream)))
;; (values (with-output-to-string (out)
;; (com.informatimago.common-lisp.cesarum.stream:copy-stream
;; (ccl:external-process-output-stream process) out))
;; (with-output-to-string (err)
;; (com.informatimago.common-lisp.cesarum.stream:copy-stream
;; (ccl:external-process-error-stream process) err)))))
;;;----------------------------------------------------------------------
(fmakunbound 'hostname)
(defun hostname (&key short long)
"RETURN: The FQDN of the local host.
SHORT: get the short name
LONG: get the fqdn (default)"
(assert (not (and short long)))
(let ((long (or long (not short))))
(string-trim #(#\newline)
(or
(unless short
(ignore-errors (shell-command-to-string "hostname --fqdn")))
(unless short
(ignore-errors (shell-command-to-string "hostname -f")))
(unless short
(ignore-errors (shell-command-to-string "hostname --long")))
(unless long
(ignore-errors (shell-command-to-string "hostname -s")))
(unless long
(ignore-errors (shell-command-to-string "hostname")))
"localhost"))))
;;;----------------------------------------------------------------------
(define-symbol-macro self -)
(define-symbol-macro it *)
(define-symbol-macro this **)
(define-symbol-macro that ***)
(define-symbol-macro them /)
(define-symbol-macro these //)
(define-symbol-macro those ///)
(define-symbol-macro is +)
(define-symbol-macro was ++)
(define-symbol-macro were +++)
(defvar *inp* (make-synonym-stream '*standard-input*) "Synonym to *standard-input*")
(defvar *out* (make-synonym-stream '*standard-output*) "Synonym to *standard-output*")
;; ;; Patch swank:
(defvar *swank-patched-p* nil)
(defun check-and-patch-swank ()
(when (and (member :swank *features*)
(find-package "SWANK")
(not (eq :external (find-symbol "*EVAL-REGION-STREAM*" "SWANK")))
(not *swank-patched-p*))
(let ((package (find-package "SWANK")))
(unless package
(error "No package named SWANK. Go home and reconsider your life!"))
(let ((var (intern "*EVAL-REGION-STREAM*" package)))
(proclaim `(special ,var))
(setf (symbol-value var) nil)
(export var package))
(let ((*package* package))
(eval (read-from-string "
(defslimefun eval-and-grab-output (string)
(with-buffer-syntax ()
(with-retry-restart (:msg \"Retry SLIME evaluation request.\")
(let* ((*eval-region-stream* (make-string-output-stream))
(*standard-output* *eval-region-stream*)
(values (multiple-value-list (eval (from-string string)))))
(list (get-output-stream-string *eval-region-stream*)
(format nil \"~{~S~^~%~}\" values))))))"))
(eval (read-from-string "
(defun eval-region (string)
\"Evaluate STRING.
Return the results of the last form as a list and as secondary value the
last form.\"
(with-input-from-string (*eval-region-stream* string)
(let (- values)
(loop
(let ((form (read *eval-region-stream* nil *eval-region-stream*)))
(when (eq form *eval-region-stream*)
(finish-output)
(return (values values -)))
(setq - form)
(setq values (multiple-value-list (eval form)))
(finish-output))))))"))))
(setf *swank-patched-p* t)))
(check-and-patch-swank)
(defun command-read-arguments ()
"Read the arguments following the expression that has just been read and that is being evaluated."
(flet ((read-arguments (stream)
(loop :for argument := (read stream nil stream)
:until (eq argument stream)
:collect argument)))
(if (and (member :swank *features*)
(find-package "SWANK")
(eq :external (nth-value 1 (find-symbol "*EVAL-REGION-STREAM*" "SWANK")))
(streamp (symbol-value (intern "*EVAL-REGION-STREAM*" "SWANK"))))
(read-arguments (symbol-value (intern "*EVAL-REGION-STREAM*" "SWANK")))
(with-input-from-string (stream (if (listen *standard-input*)
(read-line *standard-input*)
""))
(read-arguments stream)))))
(defun call-command (command)
"
DO: Call the command with the arguments read from the following line.
RETURN: No value.
"
(apply command (command-read-arguments))
(values))
(defvar *commands* '()
"List of commands, with their lambda-list and docstring.")
(defmacro command-error-handling (&body body)
`(handler-case
(progn ,@body)
(error (err)
(format *error-output* "~&~A~%" err))))
(defun import-commands ()
;; See forward-command-{macro,function}
;; (use-package '("COM.INFORMATIMAGO.COMMON-LISP.INTERACTIVE.BROWSER"
;; "COM.INFORMATIMAGO.COMMON-LISP.INTERACTIVE.INTERACTIVE"))
(dolist (command (mapcar (function first) *commands*))
(let ((new-name (intern (symbol-name command))))
(unless (eq new-name command)
(setf (symbol-function new-name) (symbol-function command))
(eval `(define-symbol-macro ,new-name (call-command ',command)))))))
(defmacro define-command (name (&rest lambda-list) &body docstring-declarations-and-body)
"
DO: Define a REPL command that can be invoked by name, without
surrounding it with parentheses.
NOTE: Commands are defined as functions, associated with a
symbol-macro expanding to a call-command call.
Call-command will parse the rest of the line as arguments
for the command. Errors are handled and printed. No
value is returned. Commands must print their results.
EXAMPLE:
(define-command cmdt (&rest arguments)
(prin1 (append arguments arguments))
(terpri))
cl-user> cmdt 1 2 3
(1 2 3 1 2 3)
; No value
cl-user>
"
(multiple-value-bind (docstrings declarations body)
(parse-body :lambda docstring-declarations-and-body)
`(progn
(setf *commands* (cons (list ',name ',lambda-list ',(car docstrings))
(delete ',name *commands* :key (function first))))
(defun ,name ,lambda-list
,@docstrings ,@declarations
(command-error-handling ,@body))
(define-symbol-macro ,name (call-command ',name)))))
(defun fmt-lambda-list (stream lambda-list at colon &rest parameters)
"Formats a lambda list using [] for optional and parameters."
(declare (ignore at colon parameters))
(let ((lh (make-help (parse-lambda-list lambda-list :ordinary))))
(write-string