8

The only thing I don't like about Emacs is the lack of namespaces, so I'm wondering if I can implement them on my own.

This is my first attempt, and it's obvious that I can't just replace every match of a name with its prefixed version, but what should I check? I can check for bindings with (let) then mark the entire subtree, but what if somebody creates a (my-let) function that uses let? Is my effort destined to fail? :(

Also, why are my defuns failing to define the function? Do I have to run something similar to intern-symbol on every new token?

Thanks!

3
  • 1
    What do you mean by namespaces in a text editor that is Emacs? Or is it the language elisp that you are actually referring to?
    – vpit3833
    Dec 11, 2010 at 23:54
  • 5
    Given that elisp lacks namespaces, and the question links to some elisp code and asks about elisp forms and functions like let, defun, and intern-symbol, I think it's safe to assume that the question is about elisp.
    – Porculus
    Dec 12, 2010 at 1:08
  • @vpit3833: That looks pretty nice! Feb 6, 2013 at 14:12

4 Answers 4

5

Since this is the first google result for elisp namespaces...

There's a minimalist implementation of namespaces called fakespace which you can get on elpa, which does basic encapsulation. I'm working on something ambitious myself, which you can check out here.

14
  • Both fakespace and your namespaces rely on side-effects rather than relying on scoping. From that point of view, I much prefer vpit3833's approach.
    – Stefan
    Nov 7, 2012 at 15:58
  • 1
    Yes. I don’t like it when developers try to make the private stuff inaccessible. I would prefer just defining an infix which tells other developers that those parts are not part of the external API. No need to shackle others if you can advise them. Feb 7, 2013 at 10:19
  • 1
    after all, you cannot actually enforce anything if people can access your code. So there’s no point in trying (and risking collateral damage). In proprietary space that might make sense (enforce restrictions on your users), but here, where everyone can read the source with C-h f <function>, C-x C-o C-s in `RET, that’s just encumumbrance, I think. Feb 7, 2013 at 10:24
  • 1
    I use the loaded term, to emphasize the problem :). Why do you want guarantees of immutability and privacy for your vars, if someone else uses them? The user of your library might have perfectly valid reasons for violating those guarantees, so to me the wish to forbid changes seems to be motivated by fear instead of seeking advantages. Feb 7, 2013 at 16:46
  • 1
    Essentially you get all the advantages you seek by just marking a variable as private, so other developers see that it might not be safe to change these. Feb 7, 2013 at 16:48
3

To handle things like my-let or my-defun, you need to macroexpand those definitions, e.g. with macroexpand-all.

For the failure to define the functions, you need to use intern instead of make-symbol (because make-symbol always creates a new distinct fresh uninterned symbol).

1

Adding namespaces will take more than prefixing the identifiers with the namespace names. The interpreter has to be able to tell the namespaces. Some tinkering must go into the interpreter as well. That might need to go through a thorough discussion at gnu.emacs.sources and/or #emacs at irc.freenode.org.

1
  • 1
    No, he uses a macro, and the interpreter expands macros just as well as the compiler, so his approach can definitely work.
    – Stefan
    Nov 7, 2012 at 15:40
0

This is a fixed version of the code from @vpit3833 to provide namespace support (using the hint from @Stefan). It’s too good to leave around half-fixed :)

;; Simple namespace definitions for easier elisp writing and clean
;; access from outside. Pythonesque elisp :)
;; 
;; thanks to vpit3833 → http://6e5e5ae9206fa093.paste.se/

(defmacro namespace (prefix &rest sexps)
  (let* ((naive-dfs-map
          (lambda (fun tree)
            (mapcar (lambda (n) (if (listp n) (funcall naive-dfs-map fun n)
                                  (funcall fun n))) tree)))
         (to-rewrite (loop for sexp in sexps
                           when (member (car sexp)
                                        '(defvar defmacro defun))
                           collect (cadr sexp)))
         (fixed-sexps (funcall naive-dfs-map
                               (lambda (n) (if (member n to-rewrite)
                                               (intern
                                                (format "%s-%s" prefix n)) n))
                               sexps)))
    `(progn ,@fixed-sexps)))

;; (namespace test
;;            (defun three () 3)
;;            (defun four () (let ((three 4)) three))
;;            (defun + (&rest args) (apply #'- args)))

;; (test-+ 1 2 3)

(provide 'namespace)
7
  • Chris Barrett suggested adding (declare (indent defun)) to the macro, so the code does not get indented that deeply after declaring a namespace. I like the deep indent, though - but I also do not have enough reputation to accept it and adapt it to show it as option (commented out). Feb 7, 2013 at 10:16
  • 1
    Well, this won't work. What you actually need is a so-called code-walker. The code-walker is a piece of Lisp code, which actually understands Lisp syntax. You need to walk the code and replace function calls and/or variable names. But only those. (macroexpand '(namespace test (defun test () 'test))) -> The last test should not be changed. Feb 9, 2013 at 14:25
  • Do you mean something like this? merl.com/publications/TR1993-017 (Macroexpand-All) Mar 25, 2013 at 11:15
  • the code actually works, though. It definitely has weaknesses (and it would be really cool if you could fix it to work in general instead of just working for simple cases), but it provides a feature I was missing a lot when writing Emacs Lisp. Mar 25, 2013 at 11:16
  • 1
    What does it mean, 'it works'? It rewrites not only functions, but also variables and data. Looks like a bug to me. (namespace test (defun foo (a) (cond ((eq a 'foo) t) ((eq a 'bar) t) (t nil)))). Now (test-foo 'bar) returns t, but (test-foo 'foo) returns nil? Mar 25, 2013 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.