drain wrote:
> Is there a function that prompts the user in the mini-buffer, matches the
> string input with a local variable name, and then returns the variable?
Francesco Mazzoli <f...@mazzo.li> writes:
> At Wed, 10 Oct 2012 00:24:35 -0700 (PDT),
> drain wrote:
>> Is there a function that prompts the user in the mini-buffer, matches the
>> string input with a local variable name, and then returns the variable?
>> At Wed, 10 Oct 2012 00:24:35 -0700 (PDT),
>> drain wrote:
>>> Is there a function that prompts the user in the mini-buffer, matches the
>>> string input with a local variable name, and then returns the variable?
If you're looking for a command:
Francesco Mazzoli suggested `(interactive "v...")' for a command, and Tassilo Horn suggested using the help system: `C-h v <variable-name> RET'. There's also `M-x apropos', which will match regular expressions.
All of those make sense if this is for *your* consumption.
If you are looking for a function:
I don't know of any such function right off. You might want to learn about `obarray' and `(mapatoms)'.
> -----Original Message-----
> From: help-gnu-emacs-bounces+dougl=shubertticketing....@gnu.org
> [mailto:help-gnu-emacs-bounces+dougl=shubertticketing....@gnu.org] On
> Behalf Of drain
> Sent: Wednesday, 2012 October 10 03:25
> To: Help-gnu-em...@gnu.org
> Subject: reading a variable from the user
> Is there a function that prompts the user in the mini-buffer, matches
> the
> string input with a local variable name, and then returns the variable?
> Is there a function that prompts the user in the mini-buffer, > matches the string input with a local variable name, and then
> returns the variable?
Not sure what you need. There is function `read-variable'.
That works only for user options, in spite of the misleading name.
See also function `read-any-variable' in library `strings.el':
(defun read-any-variable (prompt &optional default-value)
"Read name of a variable and return it as a symbol.
Unlike `read-variable', which reads only user options, this reads the
name of any variable.
Prompts with arg string PROMPT. By default, return DEFAULT-VALUE if
non-nil. If DEFAULT-VALUE is nil and the nearest symbol to the cursor
is a variable, then return that by default."
(let ((symb (cond ((fboundp 'symbol-nearest-point)
(symbol-nearest-point))
((fboundp 'symbol-at-point)
(symbol-at-point))
(t nil)))
(enable-recursive-minibuffers t))
(when (and default-value (symbolp default-value))
(setq default-value (symbol-name default-value)))
(intern (completing-read
prompt obarray 'boundp t nil
'minibuffer-history
(or default-value
(and (boundp symb) (symbol-name symb)))
t))))
There is also function `read-var-and-value' in library `simple+.el'. It does
this:
"Read a variable name and value.
READ-VAR-FN is a function to read the variable name.
SET-VAR-HIST-VAR is a variable holding a history of variable values.
MAKE-LOCAL-P non-nil means the variable is to be local.
Optional arg BUFFER is the buffer used to determine the current value
of the variable, which is used as the default value when reading the new value."
http://www.emacswiki.org/emacs-en/download/simple%2b.el
strings.el and simple+.el are new to me: thanks. They may contain the answer,
and I need to pick through them carefully, but I'll just be forthright about
what I am trying to do. Very new to Emacs Lisp...
I want to prompt myself with "To:", enter a contact name and a topic, then
pass them as arguments to compose-mail.
This was no problem, but the goal right now is to cut out the redundant
conditional statement, and instead match the name string I enter to its
variable directly, for example "William" to the William variable. Might
require pointers or arrays of some sort, but I haven't gotten that far in
the Emacs Lisp Intro.
On Thu, Oct 11 2012, drain wrote:
> strings.el and simple+.el are new to me: thanks. They may contain the answer,
> and I need to pick through them carefully, but I'll just be forthright about
> what I am trying to do. Very new to Emacs Lisp...
> I want to prompt myself with "To:", enter a contact name and a topic, then
> pass them as arguments to compose-mail.
> This was no problem, but the goal right now is to cut out the redundant
> conditional statement, and instead match the name string I enter to its
> variable directly, for example "William" to the William variable. Might
> require pointers or arrays of some sort, but I haven't gotten that far in
> the Emacs Lisp Intro.
There are many, many ways to do this obviously (hash tables, BBDB, etc),
but for the purposes of learning elisp it might be best to start with
some kind of association list (alist, section 5.8 of the Elisp manual).
That would look like this:
The difference, as far as I know, is that in the first method you can
have multiple values per list (so something like (william "William"
"White" "x...@xxxxxx.org")), whereas the dotted cons cell notation (the
second one) only allows two atoms.
If you look at the association list section of the manual, you'll see a
bunch of functions for working with lists like these. The main one is
`assoc':
It's generally better to use a `let' instead of a `setq' to make
temporary variables in your functions. Here I've used let*, which is
only different in that it evaluates the statements one by one, so later
statements can refer to values established in earlier statements.
As far as I can tell you can't use symbols in the assoc list set up by
`let' (is this wrong?). So your function might look like:
The next thing to do, for the sake of usability, would be to choose a
name using `completing-read', so that you could type a few letters and
use TAB to complete.
Hope that's useful,
Eric
-- GNU Emacs 24.2.50.1 (i686-pc-linux-gnu, GTK+ Version 3.4.4)
of 2012-10-10 on pellet
On Wednesday, October 10, 2012, Eric Abrahamsen <e...@ericabrahamsen.net>
wrote:
> The difference, as far as I know, is that in the first method you can
> have multiple values per list (so something like (william "William"
> "White" "x...@xxxxxx.org")), whereas the dotted cons cell notation (the
> second one) only allows two atoms.
FYI, '(william "William" "White" "x...@xxxxxx.org") is exactly the same as
'(william . ("William" "White" "x...@xxxxxx.org"))
For that matter it's the same as '(william . ("William" . ("White" . ("
x...@xxxxxx.org" . nil)))). A list is just a chain of cons cells.
-- -PJ
Gehm's Corollary to Clark's Law: Any technology distinguishable from
magic is insufficiently advanced.
Exactly what I was looking for. And it is very useful -- indeed more
important -- that you directed me to the relevant concepts behind this
function.
. . .
Somewhat related, does anyone know how to customize the draft / reply
buffers to display only these fields, much the way the *messages* buffer is
customized with the following code:
On Thu, Oct 11 2012, PJ Weisberg wrote:
> On Wednesday, October 10, 2012, Eric Abrahamsen <
> e...@ericabrahamsen.net> wrote:
>> The difference, as far as I know, is that in the first method you
> can
>> have multiple values per list (so something like (william "William"
>> "White" "x...@xxxxxx.org")), whereas the dotted cons cell notation
> (the
>> second one) only allows two atoms.
> FYI, '(william "William" "White" "x...@xxxxxx.org") is exactly the same
> as '(william . ("William" "White" "x...@xxxxxx.org"))
> For that matter it's the same as '(william . ("William" . ("White" .
> ("x...@xxxxxx.org" . nil)))). A list is just a chain of cons cells.
Ugh, of course! If I'd thought about it for two more seconds that would
have been obvious -- that's the whole building block of lists! I wonder
if there's much practical difference between alists made of lists vs
those made of cons cells...
-- GNU Emacs 24.2.50.1 (i686-pc-linux-gnu, GTK+ Version 3.4.4)
of 2012-10-10 on pellet