From: florian@fsavigny.de (Florian v. Savigny)
To: help-gnu-emacs@gnu.org
Subject: Choosing interactively from a list
Date: Sun, 07 Dec 2014 03:58:06 +0100 [thread overview]
Message-ID: <87bnng2mkh.fsf@bertrandrussell.Speedport_W_723V_1_36_000> (raw)
Hi there everybody,
I would like to do something which seemed easy to me at first glance,
but is now proving mysteriously difficult:
I have a list, say ("A" "B" "C" "D" ... "Z" "a" "b" "c" "d"
... "z"). I also have an index, say 13. I would like to choose an item
from this list in the minibuffer, and be able to go forward and
backwards in this list, starting from the index, i.e. here, 13.
In other words, if the list goes on alphabetically as hinted above, the
element with the index 13 would be the string "N". I would like to
have "N" as the initial input in the minibuffer, and I would like to
go back (i.e. "M", "L", "K" - you name it) with some key and forth
(i.e. "O", "P", "Q" and so on).
I tried to do this with the usual minibuffer functions, and it almost
works, but not completely as I would like it:
(defun choose-from-list (list &optional index)
"Choose an item from LIST in the minibuffer.
Use a copy of LIST as history list, i.e. do not modify LIST.
Optional arg INDEX means start in the list at index INDEX."
(let ((reverse-list (reverse list))
(reverse-ind (- (length list) index))
(history-length t))
(read-from-minibuffer "Choose: "
(nth index list)
nil
nil
(cons 'reverse-list reverse-ind))))
(setq list
(list "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
"a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"))
(choose-from-list list 13)
This does exactly what I want, i.e. hitting M-p gets me one item back
in the list; M-n gets me one item forward. However, I would like to
require that the user (me) cannot change the value from the list, only
pick it. Thus, I have tried completing-read instead of
read-from-minibuffer, because completing-read has REQUIRE-MATCH:
(defun choose-from-list-cmplr (list &optional index)
"Choose an item from LIST in the minibuffer.
Use a copy of LIST as history list, i.e. do not modify LIST.
Optional arg INDEX means start in the list at index INDEX."
(let ((reverse-list (reverse list))
(reverse-ind (- (length list) index))
(history-length t))
(completing-read "Choose: " list
nil
t
(nth index list)
(cons 'reverse-list reverse-ind))))
I imagine this should be the same thing as regards the minibuffer
history, but here, the history cycling is somehow different: Although
I get the same list element as initial input, hitting M-n now gives me
the item at the *beginning* of the list ("A"), M-p gives me the item
at the *end*.
Can somebody explain that to me? An important aspect might be that I
am not really looking for completion, but rather rigid cycling through
a fixed list. And the whole approach, i.e. using the history for
something that is not really a history (and is not even supposed to be
modified like a history variable is), reversing it etc, feels very
roundabout and not as it should be.
Many thanks in advance!
Best regards,
Florian
--
Florian von Savigny
Melanchthonstr. 41
33615 Bielefeld
next reply other threads:[~2014-12-07 2:58 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-12-07 2:58 Florian v. Savigny [this message]
2014-12-07 3:33 ` Choosing interactively from a list John Mastro
2014-12-07 10:56 ` Choosing interactively from a list (starting off at any position) Florian v. Savigny
[not found] ` <CAOj2CQQ45gZpmuQUsMNp=iokkbB1Wz2cWpy6ZKfZ=BzyoSY6xg@mail.gmail.com>
2014-12-08 19:19 ` Fwd: " John Mastro
2014-12-08 22:51 ` Florian v. Savigny
[not found] ` <<87oardah6y.fsf@bertrandrussell.Speedport_W_723V_1_36_000>
2014-12-09 2:20 ` Drew Adams
2014-12-09 19:52 ` John Mastro
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87bnng2mkh.fsf@bertrandrussell.Speedport_W_723V_1_36_000 \
--to=florian@fsavigny.de \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).