all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: John Mastro <john.b.mastro@gmail.com>
To: Help Gnu Emacs mailing list <help-gnu-emacs@gnu.org>
Cc: Oleh Krehel <ohwoeowho@gmail.com>, Kaushal Modi <kaushal.modi@gmail.com>
Subject: Re: Any infrastructure to select one of a few options in Emacs core?
Date: Thu, 19 May 2016 18:16:22 -0700	[thread overview]
Message-ID: <CAOj2CQTpTF_roGwj+xv46=EF+4UG0NedQsdsGyHJ3M_zytZZ5A@mail.gmail.com> (raw)
In-Reply-To: <87mvnlers6.fsf@mbork.pl>

Marcin Borkowski <mbork@mbork.pl> wrote:
> I went with this code:
>
> --8<---------------cut here---------------start------------->8---
> (defun select-choice (message choices)
>   "Display a buffer with MESSAGE and then CHOICES.
> Each element of CHOICES should be a cons cell, whose car is the
> result and the cdr is the description.  The user may select one
> by pressing a digit."
>   ;; TODO: This function is way too simple and not fool-proof!
>   (if (> (length choices) 9)
>       (error "Too many choices!")
>     (save-window-excursion
>       (pop-to-buffer " *amsrefs doi selection*" t t)
>       (delete-other-windows)            ; TODO: this should be fixed
>       (erase-buffer)
>       (insert message "\n\n")
>       (let ((i 0))
>         (dolist (choice choices)
>           (setq i (1+ i))
>           (insert (format "%d. %s\n\n" i (cdr choice)))))
>       (let ((key (read-key)))
>         (if (<= 49 key 57)
>             (car (nth (- key 49) choices))
>           (error "Wrong key pressed!"))))))
> --8<---------------cut here---------------end--------------->8---
>
> As it is said in the comment, it is far from ideal, but the whole
> program I've been writing for my friend is one big quick hack, so
> I didn't bother too much.

I've sometimes wanted something like this too.

I played around with a few cosmetic changes to your function and ended
up with this:

(defun read-choice (prompt choices)
  (if (> (length choices) 9)
      (error "Too many choices (max 9)")
    (let ((cursor-in-echo-area t)
          (prompt (if (get-text-property 0 'face prompt)
                      prompt
                    (propertize prompt
                                'face
                                'minibuffer-prompt))))
      (save-window-excursion
        (pop-to-buffer " *Read choice*" t t)
        (setq-local cursor-type nil)
        (erase-buffer)
        (let ((i 0))
          (dolist (choice choices)
            (setq i (1+ i))
            (insert (propertize (number-to-string i)
                                'face
                                'font-lock-variable-name-face)
                    (format " %s\n" (cdr choice))))
          (goto-char (point-min))
          (fit-window-to-buffer)
          (let ((key (- (read-key prompt) 48)))
            (if (<= 1 key i)
                (car (nth (1- key) choices))
              (user-error "Invalid key"))))))))

Same functionality but posted anyway in the spirit of sharing :)

Why did you find `delete-other-windows' necessary? (Your TODO mentions
that it should be "fixed", so I assume you would have preferred not to
use it.)

        John



  reply	other threads:[~2016-05-20  1:16 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-09 10:09 Any infrastructure to select one of a few options in Emacs core? Marcin Borkowski
2016-05-09 14:13 ` Drew Adams
2016-05-09 15:40   ` Marcin Borkowski
2016-05-09 16:37     ` Kaushal Modi
2016-05-09 17:10       ` Marcin Borkowski
2016-05-09 17:27         ` Drew Adams
2016-05-09 18:08           ` Marcin Borkowski
2016-05-09 18:16             ` Drew Adams
2016-05-19 20:06               ` Marcin Borkowski
2016-05-20  1:16                 ` John Mastro [this message]
2016-05-20 23:52                   ` John Mastro
2016-05-21 20:48                     ` Marcin Borkowski
2016-05-09 18:36             ` Pascal J. Bourguignon
2016-05-14  4:20               ` Emanuel Berg
2016-05-21 20:49               ` Marcin Borkowski
2016-05-14  4:18 ` Emanuel Berg
2016-05-21 20:52   ` Marcin Borkowski
     [not found]   ` <mailman.43.1463863961.1216.help-gnu-emacs@gnu.org>
2016-05-22  6:32     ` Emanuel Berg

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAOj2CQTpTF_roGwj+xv46=EF+4UG0NedQsdsGyHJ3M_zytZZ5A@mail.gmail.com' \
    --to=john.b.mastro@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=kaushal.modi@gmail.com \
    --cc=ohwoeowho@gmail.com \
    /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.
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.