Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial commit of partially working code.

  • Loading branch information...
commit 63f2281a51899ecd866273c607fe69afafd282c0 0 parents
Christopher Wellons authored

Showing 2 changed files with 91 additions and 0 deletions. Show diff stats Hide diff stats

  1. +40 0 example.el
  2. +51 0 fakespace.el
40 example.el
... ... @@ -0,0 +1,40 @@
  1 +(require 'fakespace)
  2 +
  3 +;; Start be declaring a package. `defpackage' currently supports :use
  4 +;; and :export. Anything listed in :use will be `require'd. You can
  5 +;; make your own calls to require, but they should occur before
  6 +;; `defpackage'. Otherwise the functions and variables defined in the
  7 +;; require will become part of your package and won't get exported.
  8 +
  9 +(defpackage example
  10 + (:use cl ido)
  11 + (:export example-main example-var))
  12 +
  13 +;; Caveat: any functions or variables you declare *will* be defined in
  14 +;; the main namespace (we're faking namespaces here), but the
  15 +;; non-exported ones will be removed later. They can be redefined
  16 +;; elsewhere without interfering with the definitions here.
  17 +
  18 +(defvar my-var 100
  19 + "A hidden variable.")
  20 +
  21 +(defvar example-var nil
  22 + "A public variable.")
  23 +
  24 +(defun my-func ()
  25 + "A private function."
  26 + my-var)
  27 +
  28 +(defun example-main ()
  29 + "An exported function. Notice we can access all the private
  30 +variables and functions from here."
  31 + (interactive)
  32 + (list (list (my-func) my-var) example-var
  33 + (ido-completing-read "New value: " (list "foo" "bar"))))
  34 +
  35 +;; Unlike Common Lisp, rather than declaring your namespace with
  36 +;; `in-package' you must end your package definition with
  37 +;; `end-package'. This will hide all of your internal functions away
  38 +;; from the main namespace.
  39 +
  40 +(end-package)
51 fakespace.el
... ... @@ -0,0 +1,51 @@
  1 +;;; fakespace.el --- fake Emacs lisp namespaces
  2 +
  3 +
  4 +;;; Code:
  5 +
  6 +(defun atom-list (&optional ob)
  7 + "Return given obarray OB as a list. Defaults to obarray."
  8 + (let ((lst ()))
  9 + (mapatoms (lambda (s) (push s lst)) ob)
  10 + lst))
  11 +
  12 +(defun atom-difference (a b)
  13 + "Like set-difference, but, for performance reaons, requires
  14 +specially formed lists. Returns items that are in B and not A."
  15 + (let ((diff))
  16 + (while (and (not (null a)) (not (null b)))
  17 + (while (not (eq (car a) (car b)))
  18 + (push (car b) diff)
  19 + (setq b (cdr b)))
  20 + (setq a (cdr a))
  21 + (setq b (cdr b)))
  22 + diff))
  23 +
  24 +(defun package-hide-p (s)
  25 + "Return t if this symbol should be uninterned."
  26 + (or (functionp s)
  27 + (documentation-property s 'variable-documentation)))
  28 +
  29 +(defvar old-obarray ()
  30 + "List of all the items from obarray at some previous time.")
  31 +
  32 +(defmacro defpackage (name &rest args)
  33 + (let ((code (list 'progn)))
  34 + (dolist (arg args)
  35 + (let ((type (car arg)))
  36 + (cond ((eq type :exports) t) ; interning the symbols is enough
  37 + ((eq type :use)
  38 + (setq code (append code (mapcar
  39 + (lambda (s)
  40 + `(require (quote ,s)))
  41 + (cdr arg))))))))
  42 + (setq old-obarray (atom-list))
  43 + (append code (list `(provide (quote ,name))))))
  44 +
  45 +(defmacro end-package ()
  46 + (cons 'progn
  47 + (mapcar (lambda (s) `(unintern (quote ,s)))
  48 + (remove-if-not 'package-hide-p
  49 + (atom-difference old-obarray (atom-list))))))
  50 +
  51 +(provide 'fakespace)

0 comments on commit 63f2281

Please sign in to comment.
Something went wrong with that request. Please try again.