Quantcast

reading a variable from the user

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

reading a variable from the user

drain
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?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: reading a variable from the user

Francesco Mazzoli
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?

You can use something like

    (defun foo (var)
      (interactive "v")
      (message "%S" (symbol-value var)))

See the help page for `interactive' for more info.

--
Francesco * Often in error, never in doubt

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: reading a variable from the user

Tassilo Horn-6
Francesco Mazzoli <[hidden email]> 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?
>
> You can use something like
>
>     (defun foo (var)
>       (interactive "v")
>       (message "%S" (symbol-value var)))
>
> See the help page for `interactive' for more info.

Or simply do `C-h v <variable-name> RET' which shows the documentation
and the current value of that variable.

Bye,
Tassilo


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re:Re: reading a variable from the user

horse_rivers




is this in gdb's  debug mode ,which is used to get  the  debugged  process's  local  varible value? 


At 2012-10-10 17:16:06,"Tassilo Horn" <[hidden email]> wrote: >Francesco Mazzoli <[hidden email]> 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?  >> >> You can use something like >> >>     (defun foo (var) >>       (interactive "v") >>       (message "%S" (symbol-value var))) >> >> See the help page for `interactive' for more info. > >Or simply do `C-h v <variable-name> RET' which shows the documentation >and the current value of that variable. > >Bye, >Tassilo > >


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

RE: reading a variable from the user

Doug Lewan
In reply to this post by drain
Drain,

Are you looking for a command or a function?

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)'.

I hope this helps.

,Doug

> -----Original Message-----
> From: help-gnu-emacs-bounces+dougl=[hidden email]
> [mailto:help-gnu-emacs-bounces+dougl=[hidden email]] On
> Behalf Of drain
> Sent: Wednesday, 2012 October 10 03:25
> To: [hidden email]
> 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?
>
>
>
> --
> View this message in context:
> http://emacs.1067599.n5.nabble.com/reading-a-variable-from-the-user-
> tp266778.html
> Sent from the Emacs - Help mailing list archive at Nabble.com.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

RE: reading a variable from the user

Drew Adams
In reply to this post by drain
> 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))))

http://www.emacswiki.org/emacs-en/download/strings.el

If you want functions like `symbol-nearest-point' then get
http://www.emacswiki.org/emacs-en/download/thingatpt%2b.el

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



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

RE: reading a variable from the user

drain
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.

Here's what I have:

(defun custom-compose-mail (Contact Topic)
  (interactive "sTo: \nsTopic: ")
  (setq William "xx@xxxxxxxxxxx.org"
        David "xxxxxxxxxxx@gmail.com"
        Jason "xxxxxxxxxx@gmail.com"
        Carl "xxxxxx@gmail.com")
  (cond ((equal Contact "William") (setq Contact William))
        ((equal Contact "David") (setq Contact David))
        ((equal Contact "Jason") (setq Contact Jason))
        ((equal Contact "Carl") (setq Contact Carl)))
  (compose-mail Contact Topic)
  (end-of-buffer)
  (newline))
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: reading a variable from the user

Eric Abrahamsen-2
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:

(setq names '((william "[hidden email]")
              (david "[hidden email]")
              (carl "[hidden email]")))

or like this:


(setq names '((william . "[hidden email]")
              (david . "[hidden email]")
              (carl . "[hidden email]")))

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" "[hidden email]")), 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':

(assoc 'william names) -> (william "sdfsdf@sdfsdf")

(second (assoc 'william names)) -> "sdfsdf@sdfsdf"

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:

(defun custom-compose-mail (contact topic)
  (interactive "sTo: \nsTopic: ")
  (let* ((names '(("William" "[hidden email]")
                  ("David" "[hidden email]")
                  ("Jason" "[hidden email]")
                  ("Carl" "[hidden email]")))
         (contact (second (assoc contact names))))
    (compose-mail contact topic)
    (end-of-buffer)
    (newline)))

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


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: reading a variable from the user

PJ Weisberg-2
On Wednesday, October 10, 2012, Eric Abrahamsen <[hidden email]> 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" "[hidden email]")), whereas the dotted cons cell notation (the
> second one) only allows two atoms.

FYI, '(william "William" "White" "[hidden email]") is exactly the same as '(william . ("William" "White" "[hidden email]"))

For that matter it's the same as '(william . ("William" . ("White" . ("[hidden email]" . 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: reading a variable from the user

drain
In reply to this post by Eric Abrahamsen-2
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:

(setq mime-view-mailcap-files '("~/Emacs/Wanderlust/mailcap")
 
      wl-message-ignored-field-list '("^.*:")
      wl-message-visible-field-list
      '("^To:"
    "^Cc:"
    "^From:"
    "^Subject:")
       
      wl-message-sort-field-list
      '("^From"
        "^Subject"
        "^To"
        "^Cc"))

Might be too Wanderlust-specific for the Emacs list, but wl-en has yet to respond.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: reading a variable from the user

Eric Abrahamsen-2
In reply to this post by PJ Weisberg-2
On Thu, Oct 11 2012, PJ Weisberg wrote:

> On Wednesday, October 10, 2012, Eric Abrahamsen <
> [hidden email]> 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" "[hidden email]")), whereas the dotted cons cell notation
> (the
>> second one) only allows two atoms.
>
> FYI, '(william "William" "White" "[hidden email]") is exactly the same
> as '(william . ("William" "White" "[hidden email]"))
>
> For that matter it's the same as '(william . ("William" . ("White" .
> ("[hidden email]" . 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


Loading...