Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Working on compiled code too.
Browse files Browse the repository at this point in the history
  • Loading branch information
skeeto committed May 26, 2011
1 parent 63f2281 commit 09e4043
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
16 changes: 10 additions & 6 deletions example.el
@@ -1,19 +1,20 @@
(require 'fakespace)

;; Start be declaring a package. `defpackage' currently supports :use
;; and :export. Anything listed in :use will be `require'd. You can
;; make your own calls to require, but they should occur before
;; `defpackage'. Otherwise the functions and variables defined in the
;; and :export. Any libraries listed in :use will be `require'd. You
;; can make your own calls to require, but they should generally occur
;; *before* your `defpackage'. Otherwise the symbols defined in the
;; require will become part of your package and won't get exported.

(defpackage example
(:use cl ido)
(:export example-main example-var))
(:export example-main example-var eq-hello hello))

;; Caveat: any functions or variables you declare *will* be defined in
;; the main namespace (we're faking namespaces here), but the
;; non-exported ones will be removed later. They can be redefined
;; elsewhere without interfering with the definitions here.
;; non-exported symbols will be removed afterward. The same functions
;; and variables can be redefined elsewhere without interfering with
;; the definitions here.

(defvar my-var 100
"A hidden variable.")
Expand All @@ -32,6 +33,9 @@ variables and functions from here."
(list (list (my-func) my-var) example-var
(ido-completing-read "New value: " (list "foo" "bar"))))

(defun eq-hello (sym)
(eq sym 'hello))

;; Unlike Common Lisp, rather than declaring your namespace with
;; `in-package' you must end your package definition with
;; `end-package'. This will hide all of your internal functions away
Expand Down
30 changes: 12 additions & 18 deletions fakespace.el
Expand Up @@ -3,6 +3,11 @@

;;; Code:

(require 'cl)

;; Dummy call to force autoload.
(remove-if-not 'identity ())

(defun atom-list (&optional ob)
"Return given obarray OB as a list. Defaults to obarray."
(let ((lst ()))
Expand All @@ -21,31 +26,20 @@ specially formed lists. Returns items that are in B and not A."
(setq b (cdr b)))
diff))

(defun package-hide-p (s)
"Return t if this symbol should be uninterned."
(or (functionp s)
(documentation-property s 'variable-documentation)))

(defvar old-obarray ()
"List of all the items from obarray at some previous time.")

(defmacro defpackage (name &rest args)
(let ((code (list 'progn)))
(dolist (arg args)
(let ((type (car arg)))
(cond ((eq type :exports) t) ; interning the symbols is enough
((eq type :use)
(setq code (append code (mapcar
(lambda (s)
`(require (quote ,s)))
(cdr arg))))))))
(setq old-obarray (atom-list))
(append code (list `(provide (quote ,name))))))
(dolist (arg args)
(let ((type (car arg)))
(cond ((eq type :exports) t) ; interning the symbols is enough
((eq type :use) (mapcar (lambda (s) (require s)) (cdr arg))))))
(setq old-obarray (atom-list))
`(provide (quote ,name)))

(defmacro end-package ()
(cons 'progn
(mapcar (lambda (s) `(unintern (quote ,s)))
(remove-if-not 'package-hide-p
(atom-difference old-obarray (atom-list))))))
(atom-difference old-obarray (atom-list)))))

(provide 'fakespace)

0 comments on commit 09e4043

Please sign in to comment.