all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Passing a list to an interactive function
@ 2020-12-18 11:05 pietru
  2020-12-18 11:30 ` Jean Louis
  0 siblings, 1 reply; 15+ messages in thread
From: pietru @ 2020-12-18 11:05 UTC (permalink / raw)
  To: Help Gnu Emacs

I would like to pass a list to an interactive function.  How can I do that?

Sincerely
Pietro





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

* Re: Passing a list to an interactive function
  2020-12-18 11:05 Passing a list to an interactive function pietru
@ 2020-12-18 11:30 ` Jean Louis
  2020-12-18 11:38   ` pietru
  0 siblings, 1 reply; 15+ messages in thread
From: Jean Louis @ 2020-12-18 11:30 UTC (permalink / raw)
  To: pietru; +Cc: Help Gnu Emacs

* pietru@caramail.com <pietru@caramail.com> [2020-12-18 14:19]:
> I would like to pass a list to an interactive function.  How can I do that?

(funcall-interactively #'YOUR-FUNCTION '("List" 2))




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

* Re: Passing a list to an interactive function
  2020-12-18 11:30 ` Jean Louis
@ 2020-12-18 11:38   ` pietru
  2020-12-18 11:49     ` Joost Kremers
                       ` (3 more replies)
  0 siblings, 4 replies; 15+ messages in thread
From: pietru @ 2020-12-18 11:38 UTC (permalink / raw)
  To: Jean Louis; +Cc: Help Gnu Emacs



> Sent: Friday, December 18, 2020 at 12:30 PM
> From: "Jean Louis" <bugs@gnu.support>
> To: pietru@caramail.com
> Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> Subject: Re: Passing a list to an interactive function
>
> * pietru@caramail.com <pietru@caramail.com> [2020-12-18 14:19]:
> > I would like to pass a list to an interactive function.  How can I do that?
>
> (funcall-interactively #'YOUR-FUNCTION '("List" 2))

I mean, how do I define an interactive function that takes a list as argument?




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

* Re: Passing a list to an interactive function
  2020-12-18 11:38   ` pietru
@ 2020-12-18 11:49     ` Joost Kremers
  2020-12-18 11:50     ` tomas
                       ` (2 subsequent siblings)
  3 siblings, 0 replies; 15+ messages in thread
From: Joost Kremers @ 2020-12-18 11:49 UTC (permalink / raw)
  To: help-gnu-emacs


On Fri, Dec 18 2020, pietru@caramail.com wrote:
> I mean, how do I define an interactive function that takes a list as argument?

Check out the interactive codes in the Elisp manual, if you haven't found those already:

(info "(elisp) Interactive Codes")

The only codes that would allow you to do what you want are "x" and "X", which
both read a Lisp object, the difference being that "X" also evaluates it.

-- 
Joost Kremers
Life has its moments



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

* Re: Passing a list to an interactive function
  2020-12-18 11:38   ` pietru
  2020-12-18 11:49     ` Joost Kremers
@ 2020-12-18 11:50     ` tomas
  2020-12-18 11:56     ` Jean Louis
  2020-12-19  2:05     ` pietru
  3 siblings, 0 replies; 15+ messages in thread
From: tomas @ 2020-12-18 11:50 UTC (permalink / raw)
  To: help-gnu-emacs

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

On Fri, Dec 18, 2020 at 12:38:17PM +0100, pietru@caramail.com wrote:
> 
> 
> > Sent: Friday, December 18, 2020 at 12:30 PM
> > From: "Jean Louis" <bugs@gnu.support>
> > To: pietru@caramail.com
> > Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> > Subject: Re: Passing a list to an interactive function
> >
> > * pietru@caramail.com <pietru@caramail.com> [2020-12-18 14:19]:
> > > I would like to pass a list to an interactive function.  How can I do that?
> >
> > (funcall-interactively #'YOUR-FUNCTION '("List" 2))
> 
> I mean, how do I define an interactive function that takes a list as argument?

I'd guess that 'x' is the most appropriate template char. Extracted from
`interactive's docstring:

  x -- Lisp expression read but not evaluated.

Or you don't provide a string description to `interactive'. Then you
have full control over the user input (for example, if you have a
more concrete idea about how your list's members should look like:
you didn't say anything about that part :-)

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: Passing a list to an interactive function
  2020-12-18 11:38   ` pietru
  2020-12-18 11:49     ` Joost Kremers
  2020-12-18 11:50     ` tomas
@ 2020-12-18 11:56     ` Jean Louis
  2020-12-19  2:05     ` pietru
  3 siblings, 0 replies; 15+ messages in thread
From: Jean Louis @ 2020-12-18 11:56 UTC (permalink / raw)
  To: pietru; +Cc: Help Gnu Emacs

* pietru@caramail.com <pietru@caramail.com> [2020-12-18 14:38]:
> I mean, how do I define an interactive function that takes a list as argument?

I do it this way:

(defun my-function (&optional list)
  (interactive)
  continue here...

So that way I can run the function by using M-x or I can also run
it by providing the list.

If I do not do &optional function will require the list and
normally I cannot run it with M-x interactively.

Jean




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

* Re: Passing a list to an interactive function
  2020-12-18 11:38   ` pietru
                       ` (2 preceding siblings ...)
  2020-12-18 11:56     ` Jean Louis
@ 2020-12-19  2:05     ` pietru
  2020-12-19  3:25       ` Jean Louis
  3 siblings, 1 reply; 15+ messages in thread
From: pietru @ 2020-12-19  2:05 UTC (permalink / raw)
  To: pietru; +Cc: Help Gnu Emacs, Jean Louis


> Sent: Friday, December 18, 2020 at 12:38 PM
> From: pietru@caramail.com
> To: "Jean Louis" <bugs@gnu.support>
> Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> Subject: Re: Passing a list to an interactive function
>
>
>
> > Sent: Friday, December 18, 2020 at 12:30 PM
> > From: "Jean Louis" <bugs@gnu.support>
> > To: pietru@caramail.com
> > Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> > Subject: Re: Passing a list to an interactive function
> >
> > * pietru@caramail.com <pietru@caramail.com> [2020-12-18 14:19]:
> > > I would like to pass a list to an interactive function.  How can I do that?
> >
> > (funcall-interactively #'YOUR-FUNCTION '("List" 2))

Still very unsure what to do.  I want to call "M-x thermoluminescence"
but pass six numbers to the function.

(defun thermoluminesce (ta tb tlab tc td tlcd te tf tlef)
   (interactive) )

Could I simply pass six numbers separated by spaces




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

* Re: Passing a list to an interactive function
  2020-12-19  2:05     ` pietru
@ 2020-12-19  3:25       ` Jean Louis
  2020-12-19  3:34         ` pietru
  0 siblings, 1 reply; 15+ messages in thread
From: Jean Louis @ 2020-12-19  3:25 UTC (permalink / raw)
  To: pietru; +Cc: Help Gnu Emacs

* pietru@caramail.com <pietru@caramail.com> [2020-12-19 05:06]:
> Still very unsure what to do.  I want to call "M-x thermoluminescence"
> but pass six numbers to the function.
> 
> (defun thermoluminesce (ta tb tlab tc td tlcd te tf tlef)
>    (interactive) )
> 
> Could I simply pass six numbers separated by spaces

To make it less error prone, just do this:

(defun thermoluminesce ()
  (interactive)
  (let* ((list '()) ;; here you prepare empty list
	 (list (dotimes (n 6 (reverse list))
		 (push (read-number (format "Enter number %s: " (1+ n))) list)))
	 (ta (elt list 0))
	 (tb (elt list 1))
	 (tlab (elt list 2))
	 (tc (elt list 3))
	 (td (elt list 4))
	 (tlcd (elt list 5))
	 (te (elt list 6)))
    ;; continue here using variables
    ))



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

* Re: Passing a list to an interactive function
  2020-12-19  3:25       ` Jean Louis
@ 2020-12-19  3:34         ` pietru
  2020-12-19  3:49           ` Jean Louis
  0 siblings, 1 reply; 15+ messages in thread
From: pietru @ 2020-12-19  3:34 UTC (permalink / raw)
  To: Jean Louis; +Cc: Help Gnu Emacs

I decided towards a simpler strategy by passing a string with numbers
separated by spaces.  But inside the function, I got to convert from
string to a list containing numbers.

How  could I convert to a list containing numbers.

Example of string: "3 5 13"

> Sent: Saturday, December 19, 2020 at 4:25 AM
> From: "Jean Louis" <bugs@gnu.support>
> To: pietru@caramail.com
> Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> Subject: Re: Passing a list to an interactive function
>
> * pietru@caramail.com <pietru@caramail.com> [2020-12-19 05:06]:
> > Still very unsure what to do.  I want to call "M-x thermoluminescence"
> > but pass six numbers to the function.
> >
> > (defun thermoluminesce (ta tb tlab tc td tlcd te tf tlef)
> >    (interactive) )
> >
> > Could I simply pass six numbers separated by spaces
>
> To make it less error prone, just do this:
>
> (defun thermoluminesce ()
>   (interactive)
>   (let* ((list '()) ;; here you prepare empty list
> 	 (list (dotimes (n 6 (reverse list))
> 		 (push (read-number (format "Enter number %s: " (1+ n))) list)))
> 	 (ta (elt list 0))
> 	 (tb (elt list 1))
> 	 (tlab (elt list 2))
> 	 (tc (elt list 3))
> 	 (td (elt list 4))
> 	 (tlcd (elt list 5))
> 	 (te (elt list 6)))
>     ;; continue here using variables
>     ))
>



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

* Re: Passing a list to an interactive function
  2020-12-19  3:34         ` pietru
@ 2020-12-19  3:49           ` Jean Louis
  2020-12-19  4:29             ` pietru
  0 siblings, 1 reply; 15+ messages in thread
From: Jean Louis @ 2020-12-19  3:49 UTC (permalink / raw)
  To: pietru; +Cc: Help Gnu Emacs

* pietru@caramail.com <pietru@caramail.com> [2020-12-19 06:34]:
> I decided towards a simpler strategy by passing a string with numbers
> separated by spaces.  But inside the function, I got to convert from
> string to a list containing numbers.
> 
> How  could I convert to a list containing numbers.
> 
> Example of string: "3 5 13"

Function `read-number' ensured you get the number. If function is for
you it is fine, if it is for others you risk not getting a number. If
data goes to database which also accepts string you risk losing
data as you maybe did not get a number.

If user writes spaces before numbers or after numbers or multiple
spaces between you need to remove such.

(setq my-worse-string "  1 2  3 4  5 6    ")

(setq my-string (split-string my-worse-string) ;; it will convert to numbers

my-string becomes => ("1" "2" "3" "4" "5" "6")

      Inspect `split-string' as it can omit nulls and trim
      strings automatically. But I did not here include those
      switches, as it does so by default.

Then you need to convert list of strings to list of numbers:
      
(mapcar #'string-to-number my-string)




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

* Re: Passing a list to an interactive function
  2020-12-19  3:49           ` Jean Louis
@ 2020-12-19  4:29             ` pietru
  2020-12-19  5:54               ` Jean Louis
  2020-12-19  6:23               ` Michael Heerdegen
  0 siblings, 2 replies; 15+ messages in thread
From: pietru @ 2020-12-19  4:29 UTC (permalink / raw)
  To: Jean Louis; +Cc: Help Gnu Emacs

Am trying to fill variables "ta tb tc" with the numbers from "tlist"

(defun thermoluminesce (tseq)
  "Sets thermoluminesce."
  (interactive "stseq: ")
  (message "stseq: %s " tseq)
  (let  ((slist (split-string tseq))
         tlist ta tb tpab)
    (message "slist: %s" slist)
    (message "tlist: %s" tlist)
    (message "tlen: %d" (length tlist))
    (setq tlist (mapcar #'string-to-number slist))
    ;;(setq ta (nth 1 tlist))
    ;;(setq tb (nth 2 tlist))
    ;;(setq tc (nth 3 tlist))
    (message "ta tb tc: %d %d %d" ta tb tc) ))



> Sent: Saturday, December 19, 2020 at 4:49 AM
> From: "Jean Louis" <bugs@gnu.support>
> To: pietru@caramail.com
> Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> Subject: Re: Passing a list to an interactive function
>
> * pietru@caramail.com <pietru@caramail.com> [2020-12-19 06:34]:
> > I decided towards a simpler strategy by passing a string with numbers
> > separated by spaces.  But inside the function, I got to convert from
> > string to a list containing numbers.
> >
> > How  could I convert to a list containing numbers.
> >
> > Example of string: "3 5 13"
>
> Function `read-number' ensured you get the number. If function is for
> you it is fine, if it is for others you risk not getting a number. If
> data goes to database which also accepts string you risk losing
> data as you maybe did not get a number.
>
> If user writes spaces before numbers or after numbers or multiple
> spaces between you need to remove such.
>
> (setq my-worse-string "  1 2  3 4  5 6    ")
>
> (setq my-string (split-string my-worse-string) ;; it will convert to numbers
>
> my-string becomes => ("1" "2" "3" "4" "5" "6")
>
>       Inspect `split-string' as it can omit nulls and trim
>       strings automatically. But I did not here include those
>       switches, as it does so by default.
>
> Then you need to convert list of strings to list of numbers:
>
> (mapcar #'string-to-number my-string)
>
>
>



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

* Re: Passing a list to an interactive function
  2020-12-19  4:29             ` pietru
@ 2020-12-19  5:54               ` Jean Louis
  2020-12-19 11:03                 ` pietru
  2020-12-19  6:23               ` Michael Heerdegen
  1 sibling, 1 reply; 15+ messages in thread
From: Jean Louis @ 2020-12-19  5:54 UTC (permalink / raw)
  To: pietru; +Cc: Help Gnu Emacs

* pietru@caramail.com <pietru@caramail.com> [2020-12-19 07:30]:
> Am trying to fill variables "ta tb tc" with the numbers from "tlist"
> 
(defun thermoluminesce (tseq)
  "Sets thermoluminesce."
  (interactive "stseq: ")
  (message "stseq: %s " tseq)
  (let  ((slist (split-string tseq))
         tlist ta tb tpab)
    (message "slist: %s" slist)
    (message "tlist: %s" tlist)
    (message "tlen: %d" (length tlist))
    (setq tlist (mapcar #'string-to-number slist))
    ;;(setq ta (nth 1 tlist))
    ;;(setq tb (nth 2 tlist))
    (setq tc (nth 3 tlist))
    (message "ta tb tc: %d %d %d" ta tb tc) ))

You could as well teach us here what those variables mean by giving
them better descriptive names.

(nth 2 '(1 2 3)) => 3

Is maybe that the problem? That nth is counting starting from 0, not from 1.




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

* Re: Passing a list to an interactive function
  2020-12-19  4:29             ` pietru
  2020-12-19  5:54               ` Jean Louis
@ 2020-12-19  6:23               ` Michael Heerdegen
  2020-12-19  6:26                 ` pietru
  1 sibling, 1 reply; 15+ messages in thread
From: Michael Heerdegen @ 2020-12-19  6:23 UTC (permalink / raw)
  To: help-gnu-emacs

pietru@caramail.com writes:

>     ;;(setq ta (nth 1 tlist))
>     ;;(setq tb (nth 2 tlist))
>     ;;(setq tc (nth 3 tlist))

You do not just miss the fact that the list element number index starts
from 0?

Michael.




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

* Passing a list to an interactive function
  2020-12-19  6:23               ` Michael Heerdegen
@ 2020-12-19  6:26                 ` pietru
  0 siblings, 0 replies; 15+ messages in thread
From: pietru @ 2020-12-19  6:26 UTC (permalink / raw)
  Cc: help-gnu-emacs


Have got everything working now.  Yes, had checked and start at 0.

Thanks

> Sent: Saturday, December 19, 2020 at 11:53 AM
> From: "Michael Heerdegen" <michael_heerdegen@web.de>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Passing a list to an interactive function
>
> pietru@caramail.com writes:
>
> >     ;;(setq ta (nth 1 tlist))
> >     ;;(setq tb (nth 2 tlist))
> >     ;;(setq tc (nth 3 tlist))
>
> You do not just miss the fact that the list element number index starts
> from 0?
>
> Michael.
>
>
>



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

* Re: Passing a list to an interactive function
  2020-12-19  5:54               ` Jean Louis
@ 2020-12-19 11:03                 ` pietru
  0 siblings, 0 replies; 15+ messages in thread
From: pietru @ 2020-12-19 11:03 UTC (permalink / raw)
  To: Jean Louis; +Cc: Help Gnu Emacs

Yes, that was the problem, the counting from 1.

> Sent: Saturday, December 19, 2020 at 11:24 AM
> From: "Jean Louis" <bugs@gnu.support>
> To: pietru@caramail.com
> Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> Subject: Re: Passing a list to an interactive function
>
> * pietru@caramail.com <pietru@caramail.com> [2020-12-19 07:30]:
> > Am trying to fill variables "ta tb tc" with the numbers from "tlist"
> >
> (defun thermoluminesce (tseq)
>   "Sets thermoluminesce."
>   (interactive "stseq: ")
>   (message "stseq: %s " tseq)
>   (let  ((slist (split-string tseq))
>          tlist ta tb tpab)
>     (message "slist: %s" slist)
>     (message "tlist: %s" tlist)
>     (message "tlen: %d" (length tlist))
>     (setq tlist (mapcar #'string-to-number slist))
>     ;;(setq ta (nth 1 tlist))
>     ;;(setq tb (nth 2 tlist))
>     (setq tc (nth 3 tlist))
>     (message "ta tb tc: %d %d %d" ta tb tc) ))
>
> You could as well teach us here what those variables mean by giving
> them better descriptive names.
>
> (nth 2 '(1 2 3)) => 3
>
> Is maybe that the problem? That nth is counting starting from 0, not from 1.
>
>
>



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

end of thread, other threads:[~2020-12-19 11:03 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-12-18 11:05 Passing a list to an interactive function pietru
2020-12-18 11:30 ` Jean Louis
2020-12-18 11:38   ` pietru
2020-12-18 11:49     ` Joost Kremers
2020-12-18 11:50     ` tomas
2020-12-18 11:56     ` Jean Louis
2020-12-19  2:05     ` pietru
2020-12-19  3:25       ` Jean Louis
2020-12-19  3:34         ` pietru
2020-12-19  3:49           ` Jean Louis
2020-12-19  4:29             ` pietru
2020-12-19  5:54               ` Jean Louis
2020-12-19 11:03                 ` pietru
2020-12-19  6:23               ` Michael Heerdegen
2020-12-19  6:26                 ` pietru

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.