[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Qs on obarrays
From: |
Pascal Bourguignon |
Subject: |
Re: Qs on obarrays |
Date: |
Wed, 12 Oct 2005 03:32:39 +0200 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) |
"Drew Adams" <drew.adams@oracle.com> writes:
> The Elisp manual hints that you can create and fill an obarray like this:
>
> (let ((my-obarray (make-vector 7 0)))
> (dolist (name my-names) (intern name my-obarray))
> (completing-read "XXX: " my-obarray))
>
> That works. And this also works:
>
> (let ((my-obarray (make-vector 7 0)))
> (dolist (name my-names) (intern name my-obarray))
> (all-completions "" my-obarray))
>
> However, this does not seem to work for me:
>
> (let ((my-obarray (make-vector 7 0)))
> (dolist (name my-names) (intern name my-obarray))
> (all-completions "" my-obarray 'commandp))
>
> Even if all of my-names are symbol-names of commands, this always returns
> nil. Any idea why?
Because symbols are objects, they have their own identity. When a
symbol is used to name a function or command, it's the symbol identity
that's used, not its name (otherwise we would use strings to name
functions!).
Try this:
(defmacro defun-with-symbol (name args &rest body)
`(defun ,(eval name) ,args ,@body))
(let ((my-obarray (make-vector 7 0)))
(defun-with-symbol (intern "insert-hello" my-obarray) ()
(interactive)
(insert "Hello "))
(defun-with-symbol (intern "insert-world" my-obarray) ()
(interactive)
(insert "World! "))
(funcall (intern "insert-hello" my-obarray))
(funcall (intern "insert-world" my-obarray))
(all-completions "" my-obarray 'commandp))
Of course, you may have difficulties to access to these commands since
they're interned in a different obarray. Unfortunately emacs isn't
based on Common Lisp, and doesn't have packages. In CL, one would
write (my-obarray::insert-hello) or M-x my-obarray::insert-hello RET
to call the command...
> Also, the obarray length must be supplied to make-vector. Anyone know a
> heuristic for a good vector length to use when you expect a certain number
> of symbols? I notice that the length of standard variable `obarray's value
> is 1511 (prime, presumably). If I expect on the order of N symbols, what is
> a good length to use? Or is there no simple formula? If not, anyone know a
> good length for, say, 100 symbols?
A prime number that's slightly greater than the maximum number of
symbols would be a good length.
> (BTW - The obarray vector itself does not show all of the interned symbols,
> when printed. I assume this is because if there are multiple symbols in the
> same bucket only one of them can be printed when the vector is shown.)
Indeed.
--
__Pascal Bourguignon__ http://www.informatimago.com/
The mighty hunter
Returns with gifts of plump birds,
Your foot just squashed one.
- Qs on obarrays, Drew Adams, 2005/10/11
- Re: Qs on obarrays,
Pascal Bourguignon <=
Re: Qs on obarrays, Stefan Monnier, 2005/10/12