From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.help Subject: Re: Any infrastructure to select one of a few options in Emacs core? Date: Sat, 21 May 2016 22:48:14 +0200 Message-ID: <87y473rvch.fsf@mbork.pl> References: <87shxrczlt.fsf@mbork.pl> <12f1c4cf-ee4a-444e-bfc3-1e703d3ad50d@default> <87h9e7ck96.fsf@mbork.pl> <878tzjcg3x.fsf@mbork.pl> <86f9c662-a9ec-4bf5-ab0d-8f66d4e0fcf5@default> <877ff3cdec.fsf@mbork.pl> <87mvnlers6.fsf@mbork.pl> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1463863759 20540 80.91.229.3 (21 May 2016 20:49:19 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 21 May 2016 20:49:19 +0000 (UTC) Cc: Help Gnu Emacs mailing list , Oleh Krehel , Kaushal Modi To: John Mastro Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat May 21 22:49:13 2016 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1b4Dpe-0006vl-DI for geh-help-gnu-emacs@m.gmane.org; Sat, 21 May 2016 22:49:10 +0200 Original-Received: from localhost ([::1]:41204 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b4Dpd-0005g9-DR for geh-help-gnu-emacs@m.gmane.org; Sat, 21 May 2016 16:49:09 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:43979) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b4Dow-0005dd-UN for help-gnu-emacs@gnu.org; Sat, 21 May 2016 16:48:28 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b4Doq-0002Bc-VS for help-gnu-emacs@gnu.org; Sat, 21 May 2016 16:48:25 -0400 Original-Received: from mail.mojserwer.eu ([2a01:5e00:2:52::8]:58065) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b4Doq-0002BM-Kh for help-gnu-emacs@gnu.org; Sat, 21 May 2016 16:48:20 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id 2F820B95689; Sat, 21 May 2016 22:48:18 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mail.mojserwer.eu Original-Received: from mail.mojserwer.eu ([127.0.0.1]) by localhost (mail.mojserwer.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 8vUwutm0SxYn; Sat, 21 May 2016 22:48:15 +0200 (CEST) Original-Received: from localhost (98-171.echostar.pl [213.156.98.171]) by mail.mojserwer.eu (Postfix) with ESMTPSA id 6C56AB95686; Sat, 21 May 2016 22:48:15 +0200 (CEST) User-agent: mu4e 0.9.16; emacs 25.1.50.1 In-reply-to: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 2a01:5e00:2:52::8 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:110087 Archived-At: On 2016-05-21, at 01:52, John Mastro wrote: > John Mastro wrote: > >> I played around with a few cosmetic changes to your function and ended >> up with this: > > I realized that, for my use case, it makes more sense to let the caller > specify the characters rather than automatically using the digits 1-9. > So I ended up with this instead, which is also a bit shorter and > simpler. > > (defun read-choice (prompt choices) > (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) > (pcase-dolist (`(,char ,description) choices) > (insert (propertize (string char) > 'face > 'font-lock-variable-name-face) > (format " %s\n" description))) > (goto-char (point-min)) > (fit-window-to-buffer) > (or (assoc (read-key prompt) choices) > (user-error "Invalid selection"))))) Nice, thanks! I didn't know about several things you used here - every day is an opportunity to learn! (I'll finally have to read about pcase, too.) I like especially the `fit-window-to-buffer' function - it seems it's exactly what I needed, much better than my `delete-other-windows'! Also, it seems you don't need `setq-local' here, since `cursor-type' is automatically buffer-local anyway. > In Emacs's master branch (but not the emacs-25 release branch) there's a > new function `read-multiple-choice' that's similar in purpose and worth > a look. Yes, I've seen that. Not useful for me, though -- in my use case the descriptions of the choices are too long (maybe even several lines!). > John Best, -- Marcin Borkowski http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski Faculty of Mathematics and Computer Science Adam Mickiewicz University