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
|
* The Nitty Gritty
In general, =define-namespace= should work as you expect it to. But if you
need to understand why something is or isn't being namespaced, here's
the nitty gritty of how it happens.
** Quoted Lists
Quoted lists (be it with =quote= or =function=) are namespaced *only*
of the list is a =lambda= form (or a =macro= form). Arbitrary lists
are returned untouched (they are way too arbitrary to be handled
sanely). That is:
#+begin_src emacs-lisp
(define-namespace foo-
(defun infinite (y)
(mapcar
'(lambda (x) (infinite x))
'(not a lambda (infinite x))))
)
#+end_src
expands to
#+begin_src emacs-lisp
(defun foo-infinite (y)
(mapcar
'(lambda (x) (foo-infinite x))
'(not a lambda (infinite x))))
#+end_src
Note that = '(lambda ...)= is bad practice in Emacs, you should use
=(lambda ...)= instead (which is also namespaced just fine).
** Symbols Quoted with Quote
A symbol quoted with =quote= (or = ' =) is never namespaced.
#+begin_src emacs-lisp
(define-namespace foo-
(defvar var nil)
(defvaralias 'varalias 'var)
(defun fun nil)
(defalias 'alias 'fun)
(defalias 'otheralias 'var)
)
#+end_src
expands to
#+begin_src emacs-lisp
(defvar foo-var nil)
(defvaralias 'varalias 'var)
(defun foo-fun nil)
(defalias 'alias 'fun)
;;; foo-var is not a function, so:
(defalias 'otheralias 'var)
#+end_src
If you provide the =:assume-var-quote= keyword, quoted symbols will be
namespaced as variables instead.
** Symbols Quoted with Function
A symbol quoted with =function= (or =#' =) is assumed to be the name of a
function, and will be namespaced as such if possible. You may provide
the =:dont-assume-function-quote= keyword to disable this behaviour,
=function= will then be treated like =quote=.
#+begin_src emacs-lisp
(define-namespace foo-
(defun fun (x) x)
(mapcar 'fun somelist)
(mapcar #'fun somelist)
)
#+end_src
expands to
#+begin_src emacs-lisp
(defun foo-fun (x) x)
(mapcar 'fun somelist)
(mapcar #'foo-fun somelist)
#+end_src
** Backticks (quasi-quotes)
Backticks (or =`=) are handled as you would expect. Lists or simbles
quoted with a backtick are treated the same as those quoted with
regular quotes ([[#quoted-lists][see above]]), except anything prefixed by a comma (which
is effectively not quoted) is namespaced as usual.
** Local Variables Take Precedence
Local variables take precedence over namespace variables.
For instance
#+begin_src emacs-lisp
(define-namespace foo-
(defvar bar "2")
(defun funca (bar)
(string-to-int bar))
)
#+end_src
expands to
#+begin_src emacs-lisp
(defvar foo-bar "2")
(defun funca (bar)
(string-to-int bar))
#+end_src
Note how the last =bar= isn't namespaced. That's because it is local
to the =funca= function, and takes precedence over the global
=foo-bar=. The argument list of functions, macros, and lambdas are
all local definitions.
By default, this does not happen with let bindings, they are
namespaced as usual (if the variable name in question has a global
definition). The reason is that let bindings are commonly used to
temporarily override global bindings.
You can customize this behaviour with the =:no-let-vars= keyword.
Then:
#+begin_src emacs-lisp
(define-namespace foo- :no-let-vars
(defvar bar "2")
(let ((bar "1"))
(string-to-int bar))
)
#+end_src
will expand to
#+begin_src emacs-lisp
(defvar foo-bar "2")
(let ((bar "1"))
(string-to-int bar))
#+end_src
** Macros
Macros are handled in a very intelligent manner.
*Names* needs to know which parts of a macro's arguments are
evaluatable forms, and which are just arbitrary symbols. This presents
a challenge because macro arguments could be absolutely anything.
Fortunately, (good) macros already provide that information in their
=debug= declaration.
Thus, *Names* uses the macro's =edebug-spec-list= to find out which
arguments are evaluatable forms, and namespaces only those. Other
arguments are left untouched. Usually, this is not something you'll
need to worry about, it should just do what you expect from it.
This is only relevant if you write your own macros. If you do,
remember to add a debug declaration in them.
*** The theading macros (~->~ and ~-->~)
The threading macros would require special treatment to namespace
correctly. However, you can use the ~:functionlike-macros~ keyword to
tell *Names* to treat them as regular functions.
For example, in the following snippet:
#+BEGIN_SRC emacs-lisp
(require 'dash)
(define-namespace foo-
:functionlike-macros (-> ->>)
(defvar var nil)
(defun fun (x &optional y)
(concat x y))
(-> "some string"
(fun var)
fun)
)
#+END_SRC
the ~(fun var)~ part would be namespaced prefectly fine (~fun~ and
~var~ will be identified as a function and variable respectively),
because it looks like a regular function call. However, the second use
of ~fun~ will not be correctly namespaced, because that ~fun~ looks
like a variable.
In other words, you should use these macros like this instead:
#+BEGIN_SRC emacs-lisp
(-> "some string"
(fun var)
(fun))
#+END_SRC
** Accessing Global Symbols
If one of your definitions shadows a global definition, you can still
access it by prefixing it with =::=.
#+begin_src emacs-lisp
(define-namespace foo-
(defun message ()
(message)
(::message "Hi"))
)
#+end_src
expands to
#+begin_src emacs-lisp
(defun foo-message ()
(foo-message)
(message "Hi"))
#+end_src
When in doubt feel free to use =::=, it will always get removed (as
long as it's not inside a =quote=). You may also change this prefix to
something else with the =:prefix= keyword.
|