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
|
;;; pkg.el --- Lisp packages -*- lexical-binding: t -*-
;; Copyright (C) 2022 Free Software Foundation, Inc.
;; Author: Gerd Möllmann <gerd@gnu.org>
;; Keywords: lisp, tools, maint
;; Version: 1.0
;; This file is part of GNU Emacs.
;; GNU Emacs 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 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This file is part of the implementation of Lisp packages for Emacs.
;; Code is partly adapted from CMUCL, which is in the public domain.
;; The implementation strives to do as much as possible in Lisp, not
;; C. C functions with names like 'package-%...' are defined which
;; allow low-level access to the guts of Lisp_Package objects.
;; Several variables are exposed from C that allow manipulating
;; internal state.
;; All that is dangerous :-).
;;; Code:
(require 'cl-lib)
(require 'cl-macs)
(require 'gv)
;;; Define setters for internal package details.
(gv-define-simple-setter package-%name package-%set-name)
(gv-define-simple-setter package-%nicknames package-%set-nicknames)
(gv-define-simple-setter package-%use-list package-%set-use-list)
(gv-define-simple-setter package-%shadowing-symbols
package-%set-shadowing-symbols)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helpers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun pkg--check-disjoint (&rest args)
"Check whether all given arguments specify disjoint sets of symbols.
Each argument is of the form (:key . set)."
(cl-loop for (current-arg . rest-args) on args
do
(cl-loop with (key1 . set1) = current-arg
for (key2 . set2) in rest-args
for common = (cl-delete-duplicates
(cl-intersection set1 set2 :test #'string=))
unless (null common)
do
(error "Parameters %s and %s must be disjoint \
but have common elements %s" key1 key2 common))))
(defun pkg--stringify-name (name kind)
"Return a string for string designator NAME.
If NAME is a string, return that.
If NAME is a symbol, return its symbol name.
If NAME is a character, return what `char-to-string' returns.
KIND is the kind of name we are processing, for error messages."
(cl-typecase name
(string name)
(symbol (cl-symbol-name name))
(base-char (char-to-string name))
(t (error "Bogus %s: %s" kind name))))
(defun pkg--stringify-names (names kind)
"Transform a list of string designators to a list of strings.
Duplicates are removed from the result list."
(cl-remove-duplicates
(mapcar #'(lambda (name) (pkg--stringify-name name kind)) names)
:test #'equal))
(defun pkg-package-namify (n)
"Return N as a package name."
(pkg--stringify-name n "package"))
(defun pkg-find-package (name)
"Return the package with NAME in the package registry.
Value is nil if no package is found."
(gethash name *package-registry* nil))
(defun pkg--symbol-listify (thing)
"Return a list of symbols for THING.
If THING is a list, check that all elements of the list are
symbols, and return THING.
If THING is a symbol, return a list that contains THING only.
Otherwise, signal an error."
(cond ((listp thing)
(dolist (s thing)
(unless (symbolp s)
(error "%s is not a symbol" s)))
thing)
((symbolp thing)
(list thing))
(t
(error "%s is neither a symbol nor a list of symbols" thing))))
(cl-defun pkg--find-or-make-package (name)
"Find or make a package named NAME.
If NAME is a package object, return that. Otherwise, if NAME can
be found with `find-package' return that. Otherwise, make a new
package with name NAME."
(cond ((packagep name)
(unless (package-%name name)
(error "Can't do anything with deleted package: %s" name))
name)
(t
(let* ((name (pkg--stringify-name name "package name")))
(or (pkg-find-package name)
(make-package name))))))
(defun pkg--packages-from-names (names)
"Return a list of packages object for NAMES.
NAMES must be a list of package objects or valid package names."
(mapcar #'(lambda (name) (pkg--find-or-make-package name))
names))
(defun pkg--listify-packages (packages)
"Return a list of packages for PACKAGES.
If PACKAGES is not a list, make it a list. Then, find or make
packages for packages named in the list and return the result."
(let ((packages (if (listp packages) packages (list packages))))
(cl-remove-duplicates (mapcar #'pkg--find-or-make-package
packages))))
(defun pkg--package-or-lose (name)
"Return the package denoted by NAME.
If NAME is a package, return that.
Otherwise, NAME must be the name of a registered package."
(if (packagep name)
name
(let ((pkg-name (pkg--stringify-name name "package")))
(or (find-package pkg-name)
(error "No package %s found" name)))))
(cl-defun pkg--remove-from-registry (package)
"Remove PACKAGE from the package registry."
;; Note that an unregistered package might have the same name or
;; nickname as a registered package. Prevent deleting such a
;; package from unregistering some other package.
(let ((names ()))
(maphash (lambda (n p)
(when (eq p package)
(push n names)))
*package-registry*)
(dolist (n names)
(remhash n *package-registry*))))
(defun pkg--package-or-default (package)
"Return the package object denoted by PACKAGE.
If PACKAGE is a package object, return that.
If PACKAGE is nil, return the current package.
Otherwise assume that "
(cond ((packagep package) package)
((null package) *package*)
(t (pkg--package-or-lose package))))
(defun pkg--ensure-symbol (name package)
;; We could also intern it, hm...
(cl-multiple-value-bind (symbol how)
(find-symbol name package)
(if how
symbol
(error "%s does not contain a symbol %s"
(package-name package) name))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Macros
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;###autoload
(cl-defmacro do-symbols ((var &optional (package '*package*) result-form)
&body body)
"Loop over symbols in a package.
Evaluate BODY with VAR bound to each symbol accessible in the given
PACKAGE, or the current package if PACKAGE is not specified.
Return what RESULT-FORM evaluates to, if specified, and the loop ends
normally, or else if an explcit return occurs the value it transfers."
(declare (indent 1))
(cl-with-gensyms (flet-name)
`(cl-block nil
(cl-flet ((,flet-name (,var)
(cl-tagbody ,@body)))
(let* ((package (pkg--package-or-lose ,package)))
(maphash (lambda (k _v) (,flet-name k))
(package-%symbols package))
(dolist (p (package-%use-list package))
(maphash (lambda (k v)
(when (eq v :external)
(,flet-name k)))
(package-%symbols p)))))
(let ((,var nil))
,var
,result-form))))
;;;###autoload
(cl-defmacro do-external-symbols ((var &optional (package '*package*) result-form)
&body body)
"Loop over external symbols in a package.
Evaluate BODY with VAR bound to each symbol accessible in the given
PACKAGE, or the current package if PACKAGE is not specified.
Return what RESULT-FORM evaluates to, if specified, and the loop ends
normally, or else if an explcit return occurs the value it transfers."
(cl-with-gensyms (flet-name)
`(cl-block nil
(cl-flet ((,flet-name (,var)
(cl-tagbody ,@body)))
(let* ((package (pkg--package-or-lose ,package)))
(maphash (lambda (k v)
(when (eq v :external)
(,flet-name k)))
(package-%symbols package))))
(let ((,var nil))
,var
,result-form))))
;;;###autoload
(cl-defmacro do-all-symbols ((var &optional result-form) &body body)
"Loop over all symbols in all registered packages.
Evaluate BODY with VAR bound to each symbol accessible in the given
PACKAGE, or the current package if PACKAGE is not specified.
Return what RESULT-FORM evaluates to, if specified, and the loop ends
normally, or else if an explcit return occurs the value it transfers."
(cl-with-gensyms (flet-name)
`(cl-block nil
(cl-flet ((,flet-name (,var)
(cl-tagbody ,@body)))
(dolist (package (list-all-packages))
(maphash (lambda (k _v)
(,flet-name k))
(package-%symbols package))))
(let ((,var nil))
,var
,result-form))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Basic stuff
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;###autoload
(cl-defun make-package (name &key nicknames use (size 10)
(register nil))
"Create and return a new package with name NAME.
NAME must be a string designator, that is a string, a symbol, or
a character. If it is a symbol, the symbol's name will be used
as package name. If a character, the character's string
representation will be used (`char-to-string').
NICKNAMES specifies a list of string designators for additional
names which may be used to refer to the package. Default is nil.
USE specifies zero or more packages the external symbols of which
are to be inherited by the package. See also function
`use-package'. All packages in the use-list must be either
package objects or they are looked up in the package registry
with `find-package'. If they are not found, a new package with
the given name is created.
SIZE gives the size to use for the symbol table of the new
package. Default is 10.
REGISTER if true means register the package in the package
registry.
Please note that the newly created package is not automaticall
registered in the package registry, that is it will not be found
under its names by `find-package'. Use `register-package' to
register the package. This deviates from the CLHS specification,
but is what Common Lisp implementations usually do."
(cl-check-type size natnum)
(let* ((name (pkg--stringify-name name "package name"))
(nicknames (pkg--stringify-names nicknames "package nickname"))
(use (pkg--packages-from-names use))
(package (make-%package name size)))
(setf (package-%nicknames package) nicknames
(package-%use-list package) use)
(when register
(register-package package))
package))
;;;###autoload
(defun register-package (package)
"Register PACKAGE in the package registry.
Signal an error if the name or one of the nicknames of PACKAGE
conflicts with a name already present in the registry.
Value is PACKAGE."
(let ((package (pkg--package-or-lose package)))
(cl-flet ((check (name)
(when (gethash name *package-registry*)
(error "%s conflicts with existing package" name))))
(check (package-%name package))
(mapc #'check (package-%nicknames package))
(puthash (package-%name package) package *package-registry*)
(mapc (lambda (name) (puthash name package *package-registry*))
(package-%nicknames package))
package)))
;;;###autoload
(defun unregister-package (package)
"Unregister PACKAGE from the package registry.
This removed the name of the package and all its nicknames
from *package-registry*."
(pkg--remove-from-registry (pkg--package-or-lose package)))
;;;###autoload
(defun list-all-packages ()
"Return a fresh list of all registered packages."
(let ((all ()))
(maphash (lambda (_ p) (push p all)) *package-registry*)
(cl-remove-duplicates all)))
;;;###autoload
(defun package-name (package)
"Return the name of PACKAGE.
If PACKAGE is not a package object already, it must the name of a
registered package."
(package-%name (pkg--package-or-lose package)))
;;;###autoload
(defun package-nicknames (package)
"Return the list of nickname strings of PACKAGE.
If PACKAGE is not a package object already, it must the name of a
registered package."
(package-%nicknames (pkg--package-or-lose package)))
;;;###autoload
(defun package-shadowing-symbols (package)
"Return the list of shadowing symbols of PACKAGE.
If PACKAGE is not a package object already, it must the name of a
registered package."
(package-%shadowing-symbols (pkg--package-or-lose package)))
;;;###autoload
(defun package-use-list (package)
(package-%use-list (pkg--package-or-lose package)))
;;;###autoload
(defun package-used-by-list (package)
"Return a list of packages using PACKAGE."
(let ((package (pkg--package-or-lose package))
(used-by ()))
(dolist (p (list-all-packages))
(when (memq package (package-%use-list p))
(cl-pushnew p used-by)))
used-by))
;;;###autoload
(defun find-package (package)
"Find and return the package for PACKAGE.
If PACKAGE is a package object, return that.
Otherwise, PACKAGE must be a package name, and that name
is lookup up in the package registry and the result is
returned if found.
Value is nil if no package with the given name is found. "
(if (packagep package)
package
(let ((name (pkg--stringify-name package "package name")))
(gethash name *package-registry*))))
;;;###autoload
(defun delete-package (package)
"Delete PACKAGE.
If PACKAGE is an already deleted package, return nil.
If PACKAGE is a package that is not already deleted, or PACKAGE
is a package name that is registered, delete that package by
removing it from the package registry, and return t.
After this operation completes, the home package of any symbol
whose home package had previously been package is set to nil.
That is, these symbols are now considered uninterned symbols.
An attempt to delete one of the standard packages results in an
error."
(if (and (packagep package)
(null (package-%name package)))
nil
(let ((package (pkg--package-or-lose package)))
(when (or (eq package *emacs-package*)
(eq package *keyword-package*))
(error "Cannot delete a standard package"))
(pkg--remove-from-registry package)
(setf (package-%name package) nil)
(do-symbols (sym package)
(when (eq (symbol-package sym) package)
(package-%set-symbol-package sym nil)))
t)))
;;;###autoload
(defun rename-package (package new-name &optional new-nicknames)
"Replace name and nicknames of PACKAGE with NEW-NAME and NEW-NICKNAMES.
PACKAGE must be a package object, or name a registered package.
Deleted packages cannot be renamed.
NEW-NAME must be a valid package name, a string, symbol, or
character.
Optional NEW-NICKSNAMES must be a list of valid package names.
Value is the renamed package object."
(let ((package (pkg--package-or-lose package))
(new-name (pkg--stringify-name new-name "package name"))
(new-nicknames (pkg--stringify-names new-nicknames
"package nickname")))
(unless (package-%name package)
(error "Package is deleted"))
(pkg--remove-from-registry package)
(setf (package-%nicknames package) new-nicknames)
(setf (package-%name package) new-name)
(register-package package)
package))
;;;###autoload
(defun export (symbols &optional package)
"tbd"
(let ((symbols (pkg--symbol-listify symbols))
(package (pkg--package-or-default package))
(syms ()))
;; Ignore any symbols that are already external.
(dolist (sym symbols)
(cl-multiple-value-bind (_s status)
(find-symbol (cl-symbol-name sym) package)
(unless (or (eq :external status)
(memq sym syms))
(push sym syms))))
;; Find symbols and packages with conflicts.
(let ((used-by (package-used-by-list package))
(cpackages ())
(cset ()))
(dolist (sym syms)
(let ((name (cl-symbol-name sym)))
(dolist (p used-by)
(cl-multiple-value-bind (s w)
(find-symbol name p)
(when (and w (not (eq s sym))
(not (member s (package-%shadowing-symbols p))))
(cl-pushnew sym cset)
(cl-pushnew p cpackages))))))
(when cset
(error "Exporting these symbols from the %s package: %s
results in name conflicts with these packages: %s"
(package-name package)
cset
(mapcar #'package-name cpackages))))
;; Check that all symbols are accessible.
(let ((missing ())
(imports ()))
(dolist (sym syms)
(cl-multiple-value-bind (s w)
(find-symbol (cl-symbol-name sym) package)
(cond ((not (and w (eq s sym)))
(push sym missing))
((eq w :inherited)
(push sym imports)))))
(when missing
(error "These symbols are not accessible in the %s package: %s"
(package-%name package)
missing))
;; Import
(import imports package))
;; And now, three pages later, we export the suckers.
(dolist (sym syms)
(package-%set-status sym package :external))
t))
;;;###autoload
(defun unexport (_symbols &optional package)
(setq package (pkg--package-or-default package))
(error "not yet implemented"))
;;;###autoload
(defun import (symbols &optional package)
(let ((package (pkg--package-or-default package))
(symbols (pkg--symbol-listify symbols)))
(list package symbols)))
;;;###autoload
(defun shadow (symbols &optional package)
"Make an internal symbol in PACKAGE with the same name as each of the
specified SYMBOLS, adding the new symbols to the Package-Shadowing-Symbols.
If a symbol with the given name is already present in PACKAGE, then
the existing symbol is placed in the shadowing symbols list if it is
not already present."
(let* ((package (pkg--package-or-lose package)))
(dolist (name (mapcar #'string
(if (listp symbols) symbols (list symbols))))
(cl-multiple-value-bind (sym status) (find-symbol name package)
(when (or (not status) (eq status :inherited))
(setq sym (make-symbol name))
(package-%set-symbol-package sym package)
(puthash sym :internal (package-%symbols package)))
(cl-pushnew sym (package-%shadowing-symbols package)))))
t)
;;;###autoload
(defun shadowing-import (_symbols &optional package)
(setq package (pkg--package-or-default package))
(error "not yet implemented"))
;;;###autoload
(defun use-package (use &optional package)
"Add package(s) USE the the use-list of PACKAGE.
USE may be a package or list of packages or package designators.
Optional PACKAGE specifies the PACKAGE whose use-list is
to be changed. If not specified, use the current package.
Value is t."
(let* ((package (pkg--package-or-default package))
(use (pkg--listify-packages use)))
(setf (package-%use-list package)
(cl-union (package-%use-list package)
use))
t))
;;;###autoload
(defun unuse-package (unuse &optional package)
"Remove package(s) UNUSE the the use-list of PACKAGE.
UNUSE may be a package or list of packages or package designators.
Optional PACKAGE specifies the PACKAGE whose use-list is
to be changed. If not specified, use the current package.
Value is t."
(let* ((package (pkg--package-or-default package))
(unuse (pkg--listify-packages unuse)))
(setf (package-%use-list package)
(cl-intersection (package-%use-list package)
unuse))
t))
;;;###autoload
(defun in-package* (package)
"Switch current package to PACKAGE with completion."
(interactive (list (completing-read "Package to switch to: "
*package-registry*
nil t)))
(let ((package (pkg--package-or-lose package)))
(setf *package* package)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; defpackage
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun pkg-defpackage (name nicknames size shadows shadowing-imports
use imports interns exports _doc-string)
(let ((package (or (find-package name)
(make-package name :use nil :size size
:nicknames nicknames))))
;; PKG-FIXME: What of the existing stuff does survive? Nicknames,
;; use-list, and so on.
(unregister-package package)
(register-package package)
;; Shadows and Shadowing-imports.
(let ((old-shadows (package-%shadowing-symbols package)))
(shadow shadows package)
(dolist (sym-name shadows)
(setf old-shadows (remove (find-symbol sym-name package) old-shadows)))
(dolist (simports-from shadowing-imports)
(let ((other-package (pkg--package-or-lose (car simports-from))))
(dolist (sym-name (cdr simports-from))
(let ((sym (pkg--ensure-symbol sym-name other-package)))
(shadowing-import sym package)
(setf old-shadows (remove sym old-shadows))))))
(when old-shadows
(warn "%s also shadows the following symbols: %s"
name old-shadows)))
;;Use
(let ((old-use-list (package-use-list package))
(new-use-list (mapcar #'pkg--package-or-lose use)))
(use-package (cl-set-difference new-use-list old-use-list) package)
(let ((laterize (cl-set-difference old-use-list new-use-list)))
(when laterize
(unuse-package laterize package)
(warn "%s previously used the following packages: %s"
name laterize))))
;;Import and Intern.
(dolist (sym-name interns)
(intern sym-name package))
(dolist (imports-from imports)
(let ((other-package (pkg--package-or-lose (car imports-from))))
(dolist (sym-name (cdr imports-from))
(import (list (pkg--ensure-symbol sym-name other-package))
package))))
;; Exports.
(let ((old-exports nil)
(exports (mapcar (lambda (sym-name) (intern sym-name package)) exports)))
(do-external-symbols (sym package)
(push sym old-exports))
(export exports package)
(let ((diff (cl-set-difference old-exports exports)))
(when diff
(warn "%s also exports the following symbols: %s" name diff))))
;; Documentation
;(setf (package-doc-string package) doc-string)
package))
(defmacro defpackage (package &rest options)
"Defines a new package called PACKAGE. Each of OPTIONS should be one of the
following:
(:NICKNAMES {package-name}*)
(:SIZE <integer>)
(:SHADOW {symbol-name}*)
(:SHADOWING-IMPORT-FROM <package-name> {symbol-name}*)
(:USE {package-name}*)
(:IMPORT-FROM <package-name> {symbol-name}*)
(:INTERN {symbol-name}*)
(:EXPORT {symbol-name}*)
(:DOCUMENTATION doc-string)
All options except :SIZE and :DOCUMENTATION can be used multiple times."
(let ((nicknames nil)
(size nil)
(shadows nil)
(shadowing-imports nil)
(use nil)
(use-p nil)
(imports nil)
(interns nil)
(exports nil)
(doc nil))
(dolist (option options)
(unless (consp option)
(error "Bogus DEFPACKAGE option: %s" option))
(cl-case (car option)
(:nicknames
(setf nicknames (pkg--stringify-names (cdr option) "package")))
(:size
(cond (size
(error "Can't specify :SIZE twice."))
((and (consp (cdr option))
(cl-typep (cl-second option) 'natnum))
(setf size (cl-second option)))
(t
(error "Bogus :SIZE, must be a positive integer: %s"
(cl-second option)))))
(:shadow
(let ((new (pkg--stringify-names (cdr option) "symbol")))
(setf shadows (append shadows new))))
(:shadowing-import-from
(let ((package-name (pkg--stringify-name (cl-second option) "package"))
(names (pkg--stringify-names (cddr option) "symbol")))
(let ((assoc (cl-assoc package-name shadowing-imports
:test #'string=)))
(if assoc
(setf (cdr assoc) (append (cdr assoc) names))
(setf shadowing-imports
(cl-acons package-name names shadowing-imports))))))
(:use
(let ((new (pkg--stringify-names (cdr option) "package")))
(setf use (cl-delete-duplicates (nconc use new) :test #'string=))
(setf use-p t)))
(:import-from
(let ((package-name (pkg--stringify-name (cl-second option) "package"))
(names (pkg--stringify-names (cddr option) "symbol")))
(let ((assoc (cl-assoc package-name imports :test #'string=)))
(if assoc
(setf (cdr assoc) (append (cdr assoc) names))
(setf imports (cl-acons package-name names imports))))))
(:intern
(let ((new (pkg--stringify-names (cdr option) "symbol")))
(setf interns (append interns new))))
(:export
(let ((new (pkg--stringify-names (cdr option) "symbol")))
(setf exports (append exports new))))
(:documentation
(when doc
(error "Can't specify :DOCUMENTATION twice."))
(setf doc (cl-coerce (cl-second option) 'string)))
(t
(error "Bogus DEFPACKAGE option: %s" option))))
(pkg--check-disjoint `(:intern ,@interns) `(:export ,@exports))
(pkg--check-disjoint `(:intern ,@interns)
`(:import-from ,@(apply 'append (mapcar 'cl-rest imports)))
`(:shadow ,@shadows)
`(:shadowing-import-from
,@(apply 'append (mapcar 'cl-rest shadowing-imports))))
`(cl-eval-when (compile load eval)
(pkg-defpackage ,(pkg--stringify-name package "package") ',nicknames ',size
',shadows ',shadowing-imports ',(if use-p use :default)
',imports ',interns ',exports ',doc))))
(provide 'pkg)
;;; pkg.el ends here
|