all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* reading a variable from the user
@ 2012-10-10  7:24 drain
  2012-10-10  9:08 ` Francesco Mazzoli
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: drain @ 2012-10-10  7:24 UTC (permalink / raw)
  To: Help-gnu-emacs

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.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: reading a variable from the user
  2012-10-10  7:24 reading a variable from the user drain
@ 2012-10-10  9:08 ` Francesco Mazzoli
  2012-10-10  9:16   ` Tassilo Horn
  2012-10-10 13:59 ` Doug Lewan
  2012-10-10 14:13 ` Drew Adams
  2 siblings, 1 reply; 11+ messages in thread
From: Francesco Mazzoli @ 2012-10-10  9:08 UTC (permalink / raw)
  To: Help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: reading a variable from the user
  2012-10-10  9:08 ` Francesco Mazzoli
@ 2012-10-10  9:16   ` Tassilo Horn
  2012-10-10  9:22     ` horse_rivers
  0 siblings, 1 reply; 11+ messages in thread
From: Tassilo Horn @ 2012-10-10  9:16 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re:Re: reading a variable from the user
  2012-10-10  9:16   ` Tassilo Horn
@ 2012-10-10  9:22     ` horse_rivers
  0 siblings, 0 replies; 11+ messages in thread
From: horse_rivers @ 2012-10-10  9:22 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 751 bytes --]





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" <tsdh@gnu.org> wrote:
>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? 
>>
>> 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
>
>

[-- Attachment #2: Type: text/html, Size: 1646 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: reading a variable from the user
  2012-10-10  7:24 reading a variable from the user drain
  2012-10-10  9:08 ` Francesco Mazzoli
@ 2012-10-10 13:59 ` Doug Lewan
  2012-10-10 14:13 ` Drew Adams
  2 siblings, 0 replies; 11+ messages in thread
From: Doug Lewan @ 2012-10-10 13:59 UTC (permalink / raw)
  To: drain, Help-gnu-emacs@gnu.org

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=shubertticketing.com@gnu.org
> [mailto:help-gnu-emacs-bounces+dougl=shubertticketing.com@gnu.org] On
> Behalf Of drain
> Sent: Wednesday, 2012 October 10 03:25
> To: Help-gnu-emacs@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?
> 
> 
> 
> --
> 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.




^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: reading a variable from the user
  2012-10-10  7:24 reading a variable from the user drain
  2012-10-10  9:08 ` Francesco Mazzoli
  2012-10-10 13:59 ` Doug Lewan
@ 2012-10-10 14:13 ` Drew Adams
  2012-10-11  1:09   ` drain
  2 siblings, 1 reply; 11+ messages in thread
From: Drew Adams @ 2012-10-10 14:13 UTC (permalink / raw)
  To: 'drain', Help-gnu-emacs

> 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





^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: reading a variable from the user
  2012-10-10 14:13 ` Drew Adams
@ 2012-10-11  1:09   ` drain
  2012-10-11  3:27     ` Eric Abrahamsen
  0 siblings, 1 reply; 11+ messages in thread
From: drain @ 2012-10-11  1:09 UTC (permalink / raw)
  To: Help-gnu-emacs

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




--
View this message in context: http://emacs.1067599.n5.nabble.com/reading-a-variable-from-the-user-tp266778p266847.html
Sent from the Emacs - Help mailing list archive at Nabble.com.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: reading a variable from the user
  2012-10-11  1:09   ` drain
@ 2012-10-11  3:27     ` Eric Abrahamsen
  2012-10-11  6:08       ` PJ Weisberg
  2012-10-11  6:16       ` drain
  0 siblings, 2 replies; 11+ messages in thread
From: Eric Abrahamsen @ 2012-10-11  3:27 UTC (permalink / raw)
  To: help-gnu-emacs

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 "xx@xxxxxxxxxxx.org")
	      (david "xxxxxxxxxxx@gmail.com")
	      (carl "xxxxxxxxxx@gmail.com")))

or like this:


(setq names '((william . "xx@xxxxxxxxxxx.org")
	      (david . "xxxxxxxxxxx@gmail.com")
	      (carl . "xxxxxxxxxx@gmail.com")))

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" "xx@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':

(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" "xx@xxxxxxxxxxx.org")
		  ("David" "xxxxxxxxxxx@gmail.com")
		  ("Jason" "xxxxxxxxxx@gmail.com")
		  ("Carl" "xxxxxx@gmail.com")))
	 (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




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: reading a variable from the user
  2012-10-11  3:27     ` Eric Abrahamsen
@ 2012-10-11  6:08       ` PJ Weisberg
  2012-10-11  6:50         ` Eric Abrahamsen
  2012-10-11  6:16       ` drain
  1 sibling, 1 reply; 11+ messages in thread
From: PJ Weisberg @ 2012-10-11  6:08 UTC (permalink / raw)
  To: Eric Abrahamsen; +Cc: help-gnu-emacs@gnu.org

[-- Attachment #1: Type: text/plain, Size: 706 bytes --]

On Wednesday, October 10, 2012, Eric Abrahamsen <eric@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" "xx@xxxxxx.org")), whereas the dotted cons cell notation (the
> second one) only allows two atoms.

FYI, '(william "William" "White" "xx@xxxxxx.org") is exactly the same as
'(william . ("William" "White" "xx@xxxxxx.org"))

For that matter it's the same as '(william . ("William" . ("White" . ("
xx@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.

[-- Attachment #2: Type: text/html, Size: 1105 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: reading a variable from the user
  2012-10-11  3:27     ` Eric Abrahamsen
  2012-10-11  6:08       ` PJ Weisberg
@ 2012-10-11  6:16       ` drain
  1 sibling, 0 replies; 11+ messages in thread
From: drain @ 2012-10-11  6:16 UTC (permalink / raw)
  To: Help-gnu-emacs

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.




--
View this message in context: http://emacs.1067599.n5.nabble.com/reading-a-variable-from-the-user-tp266778p266855.html
Sent from the Emacs - Help mailing list archive at Nabble.com.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: reading a variable from the user
  2012-10-11  6:08       ` PJ Weisberg
@ 2012-10-11  6:50         ` Eric Abrahamsen
  0 siblings, 0 replies; 11+ messages in thread
From: Eric Abrahamsen @ 2012-10-11  6:50 UTC (permalink / raw)
  To: help-gnu-emacs

On Thu, Oct 11 2012, PJ Weisberg wrote:

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




^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2012-10-11  6:50 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-10-10  7:24 reading a variable from the user drain
2012-10-10  9:08 ` Francesco Mazzoli
2012-10-10  9:16   ` Tassilo Horn
2012-10-10  9:22     ` horse_rivers
2012-10-10 13:59 ` Doug Lewan
2012-10-10 14:13 ` Drew Adams
2012-10-11  1:09   ` drain
2012-10-11  3:27     ` Eric Abrahamsen
2012-10-11  6:08       ` PJ Weisberg
2012-10-11  6:50         ` Eric Abrahamsen
2012-10-11  6:16       ` drain

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.