[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

tools for exporting symbols ?



    Date: 14 Aug 89 17:46:32+0200
    From: mpeter%bernina.zir.ethz.ch@Warbucks.AI.SRI.COM (Martin Peter)

    I have a utility package with flavors, methods, functions, var's und const's
    and I would like to export all (or at least most) of these symbols to other packages.
    So I have to insert an (export '(......)) form into my utility package and a
    simmilar import form in all the other packages, but since a have many symbols
    to export/import it would be nice to have some function which generates 
    such an export list automatically.

    Is there an "official way" how I can get such a list ? I know that I can
    write my own function with do-symbols but maybe there's a more ellegant
    solution.

It's usually considered better form to localize your package stuff into
a single place.  Symbolics provides a DEFPACKAGE form for this purpose,
and DEFPACKAGE was added to Common Lisp by the ANSI Lisp Committee
recently, which means it has a chance of appearing in any given
implementation at some point.

To construct an initial DEFPACKAGE form, you might consider something
like this:

  (defmacro defpackage-from-symbol-list (package-name &rest options)
    (let ((exports ())
	  (package (find-package package-name)))
      (do-local-symbols (symbol (or package
				    (error "Package ~A not found" package-name)))
	(when (or (fboundp symbol)		        ; pick up functions
		  (flavor:find-flavor symbol nil)	; and flavors
		  (sys:special-variable-p symbol))	; and special variables
	  (push symbol exports)))
      (setf exports (sort exports #'string-lessp))
      `(progn (defpackage ,(intern (package-name package))
		(:nicknames ,@(package-nicknames package))
		(:use ,@(mapcar #'package-name (package-use-list package)))
		,@options
		(:export ,@exports)))))

[I'm not sure the FBOUNDP will pick up DEFUN-IN-FLAVORs, but perhaps you
don't want to export them anyway.]

[The PROGN is so c-sh-M doesn't over-expand the macro.]

For example, in the default Genera environment, 

  (defpackage-from-symbol-list firewall) ==>
  (PROGN (DEFPACKAGE FIREWALL
	   (:NICKNAMES "FW")
	   (:USE "SYMBOLICS-COMMON-LISP")
	   (:EXPORT FW:*FUNCTION-KEYS-ENABLED* FW:*SELECT-KEYS-ENABLED*
		    FW:*SYSTEM-MENU-ENABLED*)))

I would use c-sh-M with a numeric argument to expand the macro into your
buffer, and then edit it by hand, discarding both the macro definition
and the use of it in the process.