Debian Codegraph @

src/lsp/packlib.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
6 ;;;; Copyright (c) 1990, Giuseppe Attardi.
7 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
8 ;;;;
9 ;;;; See file 'LICENSE' for the copyright details.
10
11 ;;;; package routines
12
13 (in-package "SYSTEM")
14
15 (defun find-all-symbols (string-or-symbol)
16 "Args: (string-designator)
17 Returns a list of all symbols that have the specified print name.
18 STRING-DESIGNATOR may be a symbol, in which case the print name of the symbol
19 is used."
20 (let ((symbol-name (string string-or-symbol)))
21 (mapcan #'(lambda (p)
22 (multiple-value-bind (s i)
23 (find-symbol symbol-name p)
24 (if (or (eq i :internal) (eq i :external))
25 (list s)
26 nil)))
27 (list-all-packages))))
28
29 (defun packages-iterator (packages options maybe-list)
30 (let ((all-symbols nil))
31 (when (or (atom packages) (not maybe-list))
32 (setq packages (list packages)))
33 (dolist (p packages)
34 (let ((package (si::coerce-to-package p)))
35 (multiple-value-bind (hash-ext hash-int packages-used)
36 (si::package-hash-tables package)
37 (when (member :external options)
38 (push (list package :external hash-ext) all-symbols))
39 (when (member :internal options)
40 (push (list package :internal hash-int) all-symbols))
41 (when (member :inherited options)
42 (dolist (p packages-used)
43 (push (list package :inherited (si::package-hash-tables p))
44 all-symbols))))))
45 (unless all-symbols
46 (return-from packages-iterator #'(lambda () (values nil nil nil nil))))
47 (let* ((current (pop all-symbols))
48 (package (first current))
49 (type (second current))
50 (iterator (si::hash-table-iterator (third current))))
51 (flet ((iterate ()
52 (tagbody
53 AGAIN
54 (multiple-value-bind (found key value)
55 (funcall iterator)
56 (declare (ignore key))
57 (cond
58 (found
59 (when (eq type :inherited)
60 (multiple-value-bind (s access)
61 (find-symbol (symbol-name value) package)
62 (unless (and (eq s value) (eq access type))
63 (go AGAIN))))
64 (return-from iterate (values t value type package)))
65 ((null all-symbols)
66 (return-from iterate (values nil nil nil nil)))
67 (t
68 (setq current (pop all-symbols))
69 (setq package (first current)
70 type (second current)
71 iterator (si::hash-table-iterator (third current))
72 ))))
73 (go AGAIN))))
74 #'iterate))))
75
76 (defmacro with-package-iterator ((iterator package-list &rest conditions)
77 &rest body)
78 (if conditions
79 (let ((aux (set-difference conditions '(:external :internal :inherited))))
80 (when aux
81 (signal-simple-error 'program-error nil "Clauses ~{~S~} are not allowed."
82 (list aux))))
83 (signal-simple-error 'program-error
84 nil
85 "Must supply at least one of :inherited, :external or :internal"
86 nil))
87 `(let ((,iterator (packages-iterator ,package-list ',conditions t)))
88 (macrolet ((,iterator () (list 'funcall ',iterator)))
89 ,@body)))
90
91 (defun expand-do-symbols (var package result-form body options)
92 (declare (si::c-local))
93 (let* ((i (gensym))
94 (found (gensym))
95 declaration doc)
96 (multiple-value-setq (declaration body doc)
97 (find-declarations body nil))
98 `(do* ((,i (packages-iterator ,package ',options t))
99 ,found ,var)
100 (nil)
101 ,@declaration
102 (multiple-value-setq (,found ,var) (funcall ,i))
103 (unless ,found (return ,result-form))
104 ,@body)))
105
106 (defmacro do-symbols ((var &optional (package '*package*) (result-form nil))
107 &rest body)
108 "Syntax: (do-symbols (var [package [result]])
109 {decl}* {tag | statement}*)
110 Executes STATEMENTs once for each symbol in PACKAGE (which defaults to the
111 current package), with VAR bound to the symbol. Then evaluates RESULT (which
112 defaults to NIL) and returns all values."
113 (expand-do-symbols var package result-form body '(:inherited :internal :external)))
114
115 (defmacro do-external-symbols
116 ((var &optional (package '*package*) (result-form nil)) &rest body)
117 "Syntax: (do-external-symbols (var [package [result]])
118 {decl}* {tag | statement}*)
119 Establishes a NIL block and executes STATEMENTs once for each external symbol
120 in PACKAGE (which defaults to the current package), with VAR bound to the
121 variable. Then evaluates RESULT (which defaults to NIL) and returns all
122 values."
123 (expand-do-symbols var package result-form body '(:external)))
124
125 (defmacro do-all-symbols ((var &optional (result-form nil)) &rest body)
126 "Syntax: (do-all-symbols (var [result]) {decl}* {tag | statement}*)
127 Establishes a NIL block and executes STATEMENTs once for each symbol in each
128 package, with VAR bound to the symbol. Then evaluates RESULT (which defaults
129 to NIL) and returns all values."
130 (expand-do-symbols var '(list-all-packages) result-form body '(:internal :external)))
131
132 (defun print-symbol-apropos (symbol)
133 (prin1 symbol)
134 (when (fboundp symbol)
135 (if (special-operator-p symbol)
136 (princ " Special form")
137 (if (macro-function symbol)
138 (princ " Macro")
139 (princ " Function"))))
140 (when (boundp symbol)
141 (if (constantp symbol)
142 (princ " Constant: ")
143 (princ " has value: "))
144 (prin1 (symbol-value symbol)))
145 (terpri))
146
147
148 (defun apropos (string &optional package)
149 "Args: (string &optional (package nil))
150 Prints those symbols whose print-names contain STRING as substring. If
151 PACKAGE is non-NIL, then only the specified PACKAGE is searched."
152 (setq string (string string))
153 (mapc #'print-symbol-apropos (apropos-list string package))
154 (values))
155
156 (defun apropos-list (string &optional package)
157 "Args: (string &optional (package nil))
158 Returns a list of all symbols whose print-names contain STRING as substring.
159 If PACKAGE is non-NIL, then only the specified PACKAGE is searched."
160 (sort (delete-duplicates (apropos-list-inner string package))
161 #'(lambda (s1 s2)
162 (string-lessp (prin1-to-string s1)
163 (prin1-to-string s2)))))
164
165 (defun apropos-list-inner (string package)
166 (declare (si::c-local))
167 (let* ((list '())
168 (string (string string)))
169 (cond (package
170 (dolist (p (package-use-list package))
171 (setf list (nconc (apropos-list-inner string p) list)))
172 (do-symbols (symbol package)
173 (when (search string (string symbol) :test #'char-equal)
174 (setq list (cons symbol list)))))
175 (t
176 (do-all-symbols (symbol)
177 (when (search string (string symbol) :test #'char-equal)
178 (setq list (cons symbol list))))))
179 list))
180
181
182 ;; Package local nicknames
183 (defun add-package-local-nickname (local-nickname nicknamed-package
184 &optional (package *package*))
185 (si:%add-package-local-nickname local-nickname nicknamed-package package))
186
187 (defun remove-package-local-nickname (old-nickname
188 &optional (package *package*))
189 (si:%remove-package-local-nickname old-nickname package))