all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Going through a list using the minibuffer
@ 2021-06-12 22:13 arvid-harnack
  2021-06-13  0:14 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 11+ messages in thread
From: arvid-harnack @ 2021-06-12 22:13 UTC (permalink / raw)
  To: help-gnu-emacs

Would like to have a list of strings that can be selected using the minibuffer.  And then set a variable 

number "k" that corresponds to the location of the string in the list.





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

* Re: Going through a list using the minibuffer
  2021-06-12 22:13 Going through a list using the minibuffer arvid-harnack
@ 2021-06-13  0:14 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-06-13 12:30   ` arvid-harnack
  0 siblings, 1 reply; 11+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-06-13  0:14 UTC (permalink / raw)
  To: help-gnu-emacs

arvid-harnack wrote:

> Would like to have a list of strings that can be selected
> using the minibuffer. And then set a variable
>
> number "k" that corresponds to the location of the string in
> the list.

Try this ...

;;; -*- lexical-binding: t -*-
;;;
;;; this file:
;;;   http://user.it.uu.se/~embe8573/emacs-init/string-minibuffer.el
;;;   https://dataswamp.org/~incal/emacs-init/string-minibuffer.el

(require 'cl-lib)

(defun select-list-item (lst)
  (let ((ps ""))
    (cl-loop for i from 0 to (1- (length lst))
             for l in lst
             do (setq ps (concat ps (format "%d: %s " i l))))
    (nth (read-number (concat ps "[index?] ")) lst)))

;; (select-list-item '("test" "string" "three"))


-- 
underground experts united
https://dataswamp.org/~incal




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

* Going through a list using the minibuffer
  2021-06-13  0:14 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-06-13 12:30   ` arvid-harnack
  2021-06-13 15:09     ` Yuri Khan
  0 siblings, 1 reply; 11+ messages in thread
From: arvid-harnack @ 2021-06-13 12:30 UTC (permalink / raw)
  To: moasenwood, help-gnu-emacs

It is good , but I want to have something more elaborate.



Suppose I define a list namely '("alpha" "beta" "gamma")



I would like to get the minibuffer to show me the options one by
one as the down and up arrow keys are pressed.  Then in my
function I get the equivalent position "k" of the string in the

list.



Thanks 

Arvid




From: Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Going through a list using the minibuffer
Date: 13/06/2021 02:14:59 Europe/Paris

arvid-harnack wrote:

> Would like to have a list of strings that can be selected
> using the minibuffer. And then set a variable
>
> number "k" that corresponds to the location of the string in
> the list.

Try this ...

;;; -*- lexical-binding: t -*-
;;;
;;; this file:
;;; http://user.it.uu.se/~embe8573/emacs-init/string-minibuffer.el
;;; https://dataswamp.org/~incal/emacs-init/string-minibuffer.el

(require 'cl-lib)

(defun select-list-item (lst)
(let ((ps ""))
(cl-loop for i from 0 to (1- (length lst))
for l in lst
do (setq ps (concat ps (format "%d: %s " i l))))
(nth (read-number (concat ps "[index?] ")) lst)))

;; (select-list-item '("test" "string" "three"))


-- 
underground experts united
https://dataswamp.org/~incal





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

* Re: Going through a list using the minibuffer
  2021-06-13 12:30   ` arvid-harnack
@ 2021-06-13 15:09     ` Yuri Khan
  2021-06-13 15:39       ` arvid-harnack
                         ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Yuri Khan @ 2021-06-13 15:09 UTC (permalink / raw)
  To: arvid-harnack; +Cc: help-gnu-emacs, Emanuel Berg

On Sun, 13 Jun 2021 at 20:55, <arvid-harnack@lavache.com> wrote:

> Suppose I define a list namely '("alpha" "beta" "gamma")
>
> I would like to get the minibuffer to show me the options one by
> one as the down and up arrow keys are pressed.  Then in my
> function I get the equivalent position "k" of the string in the
> list.

You basically want ‘(completing-read "Choose a Greek letter: "
'("alpha" "beta" "gamma") nil t)’, except that it will give you the
selected option value rather than its index.



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

* Going through a list using the minibuffer
  2021-06-13 15:09     ` Yuri Khan
@ 2021-06-13 15:39       ` arvid-harnack
  2021-06-13 15:57       ` Jean Louis
  2021-06-13 16:02       ` arvid-harnack
  2 siblings, 0 replies; 11+ messages in thread
From: arvid-harnack @ 2021-06-13 15:39 UTC (permalink / raw)
  To: Yuri Khan; +Cc: help-gnu-emacs, Emanuel Berg


>From: Yuri Khan <yuri.v.khan@gmail.com>
>To: arvid-harnack@lavache.com
>Subject: Re: Going through a list using the minibuffer
>Date: 13/06/2021 17:09:45 Europe/Paris
>Cc: Emanuel Berg <moasenwood@zoho.eu>;
>   help-gnu-emacs <help-gnu-emacs@gnu.org>

>On Sun, 13 Jun 2021 at 20:55, <arvid-harnack@lavache.com> wrote:

>> Suppose I define a list namely '("alpha" "beta" "gamma")
>>
>> I would like to get the minibuffer to show me the options one by
>> one as the down and up arrow keys are pressed. Then in my
>> function I get the equivalent position "k" of the string in the
>> list.

>You basically want ‘(completing-read "Choose a Greek letter: "
>'("alpha" "beta" "gamma") nil t)’, except that it will give you the
>selected option value rather than its index.

You are right.  Yes.



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

* Re: Going through a list using the minibuffer
  2021-06-13 15:09     ` Yuri Khan
  2021-06-13 15:39       ` arvid-harnack
@ 2021-06-13 15:57       ` Jean Louis
  2021-06-13 16:02       ` arvid-harnack
  2 siblings, 0 replies; 11+ messages in thread
From: Jean Louis @ 2021-06-13 15:57 UTC (permalink / raw)
  To: Yuri Khan; +Cc: arvid-harnack, help-gnu-emacs, Emanuel Berg

* Yuri Khan <yuri.v.khan@gmail.com> [2021-06-13 18:10]:
> On Sun, 13 Jun 2021 at 20:55, <arvid-harnack@lavache.com> wrote:
> 
> > Suppose I define a list namely '("alpha" "beta" "gamma")
> >
> > I would like to get the minibuffer to show me the options one by
> > one as the down and up arrow keys are pressed.  Then in my
> > function I get the equivalent position "k" of the string in the
> > list.
> 
> You basically want ‘(completing-read "Choose a Greek letter: "
> '("alpha" "beta" "gamma") nil t)’, except that it will give you the
> selected option value rather than its index.

Then using hash is possible.

Visual hash editing: Emacs Lisp package `rcd-hash-edit.el`
https://hyperscope.link/3/7/6/6/1/Emacs-Lisp-package-rcd-hash-edit-37661.html

M-x rcd-hash-edit

Enter: my-hash

- Press `a' to add new key/value to hash
- Choose type of Cons
- Enter configuration
- M-x rcd-hash-save

Example:

my-hash ⇒ #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8125 data ("config-1" '(12 3)))

Use this function to load hash:

(defun data-from-file (file)
  "Reads and returns Emacs Lisp data from FILE"
  (car
   (read-from-string
    (file-to-string file))))

For example in init file:

(setq my-hash (data-from-file "~/tmp/my-hash"))

my-hash ⇒ #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8125 data ("config-1" '(12 3)))

Use completing read with hash:

(completing-read "My config: " my-hash)

Choose a key, use key to get configuration:

In init.el:
(setq my-hash (data-from-file "~/tmp/my-hash"))

If you wish to set `my-other-variable' to the value of hash:

(defun my-config ()
  (interactive)
  (let* ((key (completing-read "Configuration? " my-hash nil t))
	 (value (gethash key my-hash)))
    (setq my-other-variable value)))

M-x my-config

- choose configuration
- let other variable be set

If you wish to edit hash again, no problem:

M-x rcd-hash-edit

add more values, or edit present values... it is like a database
form.

Save the hash:

M-x rcd-hash-save

There is no benefit nor advantage with this approach over simply
writing it in the init.el file. But the package would give more
understanding how to use keys to get values from
`completing-read'


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



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

* Going through a list using the minibuffer
  2021-06-13 15:09     ` Yuri Khan
  2021-06-13 15:39       ` arvid-harnack
  2021-06-13 15:57       ` Jean Louis
@ 2021-06-13 16:02       ` arvid-harnack
  2021-06-13 16:31         ` Yuri Khan
  2 siblings, 1 reply; 11+ messages in thread
From: arvid-harnack @ 2021-06-13 16:02 UTC (permalink / raw)
  To: Yuri Khan; +Cc: help-gnu-emacs, Emanuel Berg

Hi Yuri,



Using the command you described, how can I get tho selection into a variable

named "sel" ?



(completing-read "Choose a Greek letter: " '("alpha" "beta" "gamma") nil t)



Is like this good?



  (let* ( (gkser '("alpha" "beta" "gamma"))
     (gksel (completing-read
         "Choose a Greek letter: " gkser nil t)))
    (message "gksel %s" gksel))



Regards


From: Yuri Khan <yuri.v.khan@gmail.com>
To: arvid-harnack@lavache.com
Subject: Re: Going through a list using the minibuffer
Date: 13/06/2021 17:09:45 Europe/Paris
Cc: help-gnu-emacs <help-gnu-emacs@gnu.org>;
   Emanuel Berg <moasenwood@zoho.eu>

On Sun, 13 Jun 2021 at 20:55, <arvid-harnack@lavache.com> wrote:

> Suppose I define a list namely '("alpha" "beta" "gamma")
>
> I would like to get the minibuffer to show me the options one by
> one as the down and up arrow keys are pressed. Then in my
> function I get the equivalent position "k" of the string in the
> list.

You basically want ‘(completing-read "Choose a Greek letter: "
'("alpha" "beta" "gamma") nil t)’, except that it will give you the
selected option value rather than its index.




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

* Re: Going through a list using the minibuffer
  2021-06-13 16:02       ` arvid-harnack
@ 2021-06-13 16:31         ` Yuri Khan
  2021-06-13 19:48           ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 11+ messages in thread
From: Yuri Khan @ 2021-06-13 16:31 UTC (permalink / raw)
  To: arvid-harnack; +Cc: help-gnu-emacs, Emanuel Berg

On Sun, 13 Jun 2021 at 23:02, <arvid-harnack@lavache.com> wrote:
>
> Using the command you described, how can I get tho selection into a variable
> named "sel" ?
>
> (completing-read "Choose a Greek letter: " '("alpha" "beta" "gamma") nil t)
>
> Is like this good?
>
>   (let* ((gkser '("alpha" "beta" "gamma"))
>          (gksel (completing-read
>                  "Choose a Greek letter: " gkser nil t)))
>     (message "gksel %s" gksel))

Have you tried it? Does it work the way you want? If you have and it
does, why are you asking me?



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

* Re: Going through a list using the minibuffer
  2021-06-13 16:31         ` Yuri Khan
@ 2021-06-13 19:48           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-06-13 20:02             ` Jean Louis
  0 siblings, 1 reply; 11+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-06-13 19:48 UTC (permalink / raw)
  To: help-gnu-emacs

Yuri Khan wrote:

>> Is like this good?
>>
>>   (let* ((gkser '("alpha" "beta" "gamma"))
>>          (gksel (completing-read
>>                  "Choose a Greek letter: " gkser nil t)))
>>     (message "gksel %s" gksel))
>
> Have you tried it? Does it work the way you want? If you
> have and it does, why are you asking me?

The code looks good to me but the names are perhaps a bit
cryptic. This is Lisp, not C or Perl mind you...

The next step should be to write a defun that works for the
general case, so the prompt string and the list of options can
be provided as arguments.

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Going through a list using the minibuffer
  2021-06-13 19:48           ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-06-13 20:02             ` Jean Louis
  2021-06-13 20:13               ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 11+ messages in thread
From: Jean Louis @ 2021-06-13 20:02 UTC (permalink / raw)
  To: help-gnu-emacs

* Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2021-06-13 22:49]:
> Yuri Khan wrote:
> 
> >> Is like this good?
> >>
> >>   (let* ((gkser '("alpha" "beta" "gamma"))
> >>          (gksel (completing-read
> >>                  "Choose a Greek letter: " gkser nil t)))
> >>     (message "gksel %s" gksel))
> >
> > Have you tried it? Does it work the way you want? If you
> > have and it does, why are you asking me?
> 
> The code looks good to me but the names are perhaps a bit
> cryptic. This is Lisp, not C or Perl mind you...

Let's see (✿╹◡╹)

(defalias 'my 'let*)
(defalias '$input 'completing-read)
(defalias 'print$_ 'message)

(my  (($gkser '("alpha" "beta" "gamma"))
     ($gksel ($input "Choose a Greek letter: " $gkser nil t)))
  (print$_ "gksel %s" $gksel)) ⇒ "gksel beta"


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



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

* Re: Going through a list using the minibuffer
  2021-06-13 20:02             ` Jean Louis
@ 2021-06-13 20:13               ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 11+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-06-13 20:13 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

>> The code looks good to me but the names are perhaps a bit
>> cryptic. This is Lisp, not C or Perl mind you...
>
> Let's see (✿╹◡╹)
>
> (defalias 'my 'let*)
> (defalias '$input 'completing-read)
> (defalias 'print$_ 'message)
>
> (my  (($gkser '("alpha" "beta" "gamma"))
>      ($gksel ($input "Choose a Greek letter: " $gkser nil t)))
>   (print$_ "gksel %s" $gksel)) ⇒ "gksel beta"

Ha :)

-- 
underground experts united
https://dataswamp.org/~incal




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

end of thread, other threads:[~2021-06-13 20:13 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-06-12 22:13 Going through a list using the minibuffer arvid-harnack
2021-06-13  0:14 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-13 12:30   ` arvid-harnack
2021-06-13 15:09     ` Yuri Khan
2021-06-13 15:39       ` arvid-harnack
2021-06-13 15:57       ` Jean Louis
2021-06-13 16:02       ` arvid-harnack
2021-06-13 16:31         ` Yuri Khan
2021-06-13 19:48           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-06-13 20:02             ` Jean Louis
2021-06-13 20:13               ` Emanuel Berg via Users list for the GNU Emacs text editor

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.