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: Thu, 19 May 2016 22:06:49 +0200 Message-ID: <87mvnlers6.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> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1463688454 8664 80.91.229.3 (19 May 2016 20:07:34 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 19 May 2016 20:07:34 +0000 (UTC) Cc: Help Gnu Emacs mailing list , Oleh Krehel , Kaushal Modi To: Drew Adams Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu May 19 22:07:26 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 1b3UE9-00026s-NR for geh-help-gnu-emacs@m.gmane.org; Thu, 19 May 2016 22:07:25 +0200 Original-Received: from localhost ([::1]:51913 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3UE8-0000te-On for geh-help-gnu-emacs@m.gmane.org; Thu, 19 May 2016 16:07:24 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:45546) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3UDk-0000ru-9n for help-gnu-emacs@gnu.org; Thu, 19 May 2016 16:07:01 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b3UDg-0000jk-7L for help-gnu-emacs@gnu.org; Thu, 19 May 2016 16:07:00 -0400 Original-Received: from mail.mojserwer.eu ([2a01:5e00:2:52::8]:42886) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3UDf-0000jN-S7 for help-gnu-emacs@gnu.org; Thu, 19 May 2016 16:06:56 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id 74937B8B700; Thu, 19 May 2016 22:06:53 +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 gOjiB6TeMvwT; Thu, 19 May 2016 22:06:50 +0200 (CEST) Original-Received: from localhost (98-171.echostar.pl [213.156.98.171]) by mail.mojserwer.eu (Postfix) with ESMTPSA id A207AB8B6FE; Thu, 19 May 2016 22:06:50 +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:110067 Archived-At: On 2016-05-09, at 20:16, Drew Adams wrote: >> (ask-user-for-option "What would you like to eat?" '("spam" "ham" "eggs")) > > So this has nothing to do with Emacs _options_. > > You are asking how to present some choices to a user for > selection. Yes, sorry for the confusion. 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. Still, it would be really nice if Emacs offered a canonical (and configurable) way to do things like that. Best, -- Marcin Borkowski http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski Faculty of Mathematics and Computer Science Adam Mickiewicz University