From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: John Mastro Newsgroups: gmane.emacs.help Subject: Re: Any infrastructure to select one of a few options in Emacs core? Date: Thu, 19 May 2016 18:16:22 -0700 Message-ID: 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; charset=UTF-8 X-Trace: ger.gmane.org 1463707034 22456 80.91.229.3 (20 May 2016 01:17:14 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 20 May 2016 01:17:14 +0000 (UTC) Cc: Oleh Krehel , Kaushal Modi To: Help Gnu Emacs mailing list Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri May 20 03:17: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 1b3Z3w-0002zg-6Z for geh-help-gnu-emacs@m.gmane.org; Fri, 20 May 2016 03:17:12 +0200 Original-Received: from localhost ([::1]:52786 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3Z3v-00018G-6r for geh-help-gnu-emacs@m.gmane.org; Thu, 19 May 2016 21:17:11 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:43245) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3Z3V-00017q-FE for help-gnu-emacs@gnu.org; Thu, 19 May 2016 21:16:46 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b3Z3T-0000Q6-Ap for help-gnu-emacs@gnu.org; Thu, 19 May 2016 21:16:44 -0400 Original-Received: from mail-yw0-x231.google.com ([2607:f8b0:4002:c05::231]:33841) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3Z3T-0000Q2-5j for help-gnu-emacs@gnu.org; Thu, 19 May 2016 21:16:43 -0400 Original-Received: by mail-yw0-x231.google.com with SMTP id j74so95913103ywg.1 for ; Thu, 19 May 2016 18:16:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=v930r48MOhLjOdSMiTRpkh6OpbcopFD46GjFDloSnnU=; b=Rrud4Qn5H/pjx5Ko/0eDHkMh3Tn+LlNu1CQZlLq/vcwAlnPK/MuSuTb2YXjFC18Qld zm0hiws7i2TxZEZ5twpGX6fq3+NyT5A39QP4WQ5fLPFwxppCK7hPpXz19jZsVYsVFjku iDL49Pc6Q+I9uCjWnj1fBlE4JZXKmIPFNCcxmCpnLO73efBDPVbPBWFy9zBgTN7yaTqh qtsOmdG75X+KBErLwFoIxbyt/2LwBBv3/iHogrxn56fGoHjzvgtx738K07RMVMSsOqt7 NlaTgR3moulktqlOqBhoFORqEP33SCEiRu/Fkh6KWD3RH1T8vqSOay5sPusLLMATd9bq 7gmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=v930r48MOhLjOdSMiTRpkh6OpbcopFD46GjFDloSnnU=; b=Hsz6VWBV9cTJvhta19zy0CGJVhR7z3Vw9flQ0p6mGAavRd4S8Fqpvo6h8bH4hWRT0u GfNqh2mKYs7FXTmzzpxDFZ56hMmB41EPPPESwpHDxkRrHVfq2y0GEwQRgmbIOSvl7Rjt icl6SRxwe3X+utMxo0SyNhZpVZsuJk3XO2zrwus9notFKWwzj9Ucdw6Jwwl2qqRcFcQa rMohcPXqvPO/eNt5qv1n/fCl739HfYRpP0T5qO3Vwwf0lkYNTn2wFJ6NZHKj7Q5ul7+y /6iHXoVnnAPKIx4gaDu/nB6i81T8kU8JHYjR6wGiyLIUPbWheXLRn9ZOxlnwyawgBLIw lCSw== X-Gm-Message-State: AOPr4FW7KW8bpgfcJroJf2Wuo7FVj5RTiDe93Kgrs+48DLPT3e6zZKv95ZPAENA9mRjEZhZUmIjG31XRI8tdQQ== X-Received: by 10.37.115.201 with SMTP id o192mr185033ybc.69.1463707002462; Thu, 19 May 2016 18:16:42 -0700 (PDT) Original-Received: by 10.37.98.203 with HTTP; Thu, 19 May 2016 18:16:22 -0700 (PDT) In-Reply-To: <87mvnlers6.fsf@mbork.pl> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:4002:c05::231 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:110068 Archived-At: Marcin Borkowski 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