From mboxrd@z Thu Jan  1 00:00:00 1970
Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail
From: Spencer Baugh <sbaugh@catern.com>
Newsgroups: gmane.emacs.devel
Subject: Turning completion table lambdas into symbols
Date: Tue, 28 Nov 2023 23:36:44 +0000 (UTC)
Message-ID: <87a5qxv4bh.fsf@catern.com>
References: <87bkd3z9bi.fsf@catern.com> <87il5xlf9b.fsf@catern.com>
 <86y1esuajx.fsf@mail.linkov.net> <ierleas4fcr.fsf@janestreet.com>
 <86v89ws5t3.fsf@mail.linkov.net> <iercyw445tu.fsf@janestreet.com>
 <86v89vzf1o.fsf@mail.linkov.net> <87pm03jn3w.fsf@catern.com>
 <861qcjw3ch.fsf@mail.linkov.net> <ier1qcin8db.fsf@janestreet.com>
 <86r0ki2on3.fsf@mail.linkov.net> <ierttpdlqe4.fsf@janestreet.com>
 <86leao519y.fsf@mail.linkov.net> <87fs0wk5oq.fsf@catern.com>
 <86edgfin4v.fsf@mail.linkov.net> <87o7fhixzv.fsf@catern.com>
 <86o7fhy9ae.fsf@mail.linkov.net> <87v89ohc6f.fsf@catern.com>
 <86il5nhdvh.fsf@mail.linkov.net> <87plzuuend.fsf@catern.com>
 <86wmu1j2hh.fsf@mail.linkov.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214";
	logging-data="29197"; mail-complaints-to="usenet@ciao.gmane.io"
Cc: Spencer Baugh <sbaugh@janestreet.com>, emacs-devel@gnu.org
To: Juri Linkov <juri@linkov.net>
Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Nov 29 00:37:53 2023
Return-path: <emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org>
Envelope-to: ged-emacs-devel@m.gmane-mx.org
Original-Received: from lists.gnu.org ([209.51.188.17])
	by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
	(Exim 4.92)
	(envelope-from <emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org>)
	id 1r87eK-0007Pa-2P
	for ged-emacs-devel@m.gmane-mx.org; Wed, 29 Nov 2023 00:37:52 +0100
Original-Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <emacs-devel-bounces@gnu.org>)
	id 1r87dN-00053K-NE; Tue, 28 Nov 2023 18:36:53 -0500
Original-Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1)
 (envelope-from <bounces+21787432-489d-emacs-devel=gnu.org@em8926.catern.com>)
 id 1r87dK-00052s-8l
 for emacs-devel@gnu.org; Tue, 28 Nov 2023 18:36:50 -0500
Original-Received: from s.wfbtzhsw.outbound-mail.sendgrid.net ([159.183.224.105])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1)
 (envelope-from <bounces+21787432-489d-emacs-devel=gnu.org@em8926.catern.com>)
 id 1r87dH-0003ST-Ib
 for emacs-devel@gnu.org; Tue, 28 Nov 2023 18:36:49 -0500
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=catern.com;
 h=from:subject:in-reply-to:references:mime-version:to:cc:content-type:
 content-transfer-encoding:cc:content-type:from:subject:to;
 s=s1; bh=JBCe6pHSf2R60XbOGneURxleNyILaCtDIecKUgY/ams=;
 b=IXfSXqkGafHnRuPLl2YdZXa6e8nRmnOAh/JhyGFslXb2Nij1jMLDnW2Hb+n2i1yEhbtI
 EWd+fSgGDSq1W9WtycDWrbpeNKbf+HY4Nibbl77z/IzJvAOgFD23EJAew2staXo4NkFlUh
 579XvhxWwp6tVcKaXPOwokgdbs/y7fcXt8rwTproj8i7VV+qRRPCzTPQcOGTMBOXO1yPVw
 B8clG/51RcKDPyJo8xIZemOdKONX7VwdTiHjCQ6Yup8fsyQlCVagSsVbvZDkr3Lt7jOiVN
 ZwrKWcqi94cUPyf5nM4WAbUFd9RUry7+ejkckVZGZ28/nAihtjdXqO2f6zVKNTRQ==
Original-Received: by filterdrecv-655bd866f5-9f6nm with SMTP id
 filterdrecv-655bd866f5-9f6nm-1-6566798C-D
 2023-11-28 23:36:44.55028381 +0000 UTC m=+3646549.756343987
Original-Received: from earth.catern.com (unknown) by geopod-ismtpd-22 (SG) with ESMTP
 id 4OCSDN4GTtaa3JdUwteRRA Tue, 28 Nov 2023 23:36:44.356 +0000 (UTC)
Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=74.101.51.129;
 helo=localhost; envelope-from=sbaugh@catern.com; receiver=linkov.net 
Original-Received: from localhost (unknown [74.101.51.129])
 by earth.catern.com (Postfix) with ESMTPSA id 84DD663F88;
 Tue, 28 Nov 2023 18:36:02 -0500 (EST)
In-Reply-To: <86wmu1j2hh.fsf@mail.linkov.net>
X-SG-EID: =?us-ascii?Q?GW3oCMoYnalRiojMOuLzE6x2H5kORXvlCdz1UwQVRMVT4fbh9ODEfCogOe74cO?=
 =?us-ascii?Q?rI4e0V+MFZgakz9Re5a6=2FCgvGRRzKJgBh3SF5=2F2?=
 =?us-ascii?Q?kMYlJWK0xA5UI6nLRLUpH37dbNSJyzjc7WX9WGU?=
 =?us-ascii?Q?3Q923NIsX0Z6oX6nvqjrx5dZRwMi8hEtPeG3jcA?=
 =?us-ascii?Q?4M86+priCzi3ERv4JzGdjauwf2umVmDRMgAW4Zc?=
 =?us-ascii?Q?IutMg=2Fkt9NipDoo5niKMzaOQYTn=2FvZXWRXPS3p?=
X-Entity-ID: d/0VcHixlS0t7iB1YKCv4Q==
Received-SPF: pass client-ip=159.183.224.105;
 envelope-from=bounces+21787432-489d-emacs-devel=gnu.org@em8926.catern.com;
 helo=s.wfbtzhsw.outbound-mail.sendgrid.net
X-Spam_score_int: -20
X-Spam_score: -2.1
X-Spam_bar: --
X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,
 DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
 RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001,
 T_SCC_BODY_TEXT_LINE=-0.01,
 UNPARSEABLE_RELAY=0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-BeenThere: emacs-devel@gnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: "Emacs development discussions." <emacs-devel.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/emacs-devel>,
 <mailto:emacs-devel-request@gnu.org?subject=unsubscribe>
List-Archive: <https://lists.gnu.org/archive/html/emacs-devel>
List-Post: <mailto:emacs-devel@gnu.org>
List-Help: <mailto:emacs-devel-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/emacs-devel>,
 <mailto:emacs-devel-request@gnu.org?subject=subscribe>
Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org
Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org
Xref: news.gmane.io gmane.emacs.devel:313339
Archived-At: <http://permalink.gmane.org/gmane.emacs.devel/313339>

Juri Linkov <juri@linkov.net> writes:
>>> display-buffer has a similar problem, but the difference is that
>>> it's possible to identify a buffer by its name and use a regexp
>>> to match buffer names.  Whereas for completing-read it's not easy
>>> to identify a completion table.  A category matches a set of
>>> completion tables, so maybe we need another identification
>>> for individual tables?
>>
>> True, that would help.  Maybe the function symbol for the completion
>> table?  Tables are usually lambdas today, but maybe we could make it
>> easy to use a defun'd function instead, which would be very good for
>> comprehensibility in general IMO.
>
> Turning lambdas into symbols looks good, this is like the existing
> 'help--symbol-completion-table'.

Like 'help--symbol-completion-table' in what way?

One thing that requires a lambda is when the table is over some custom
data.  But that can sometimes be avoided by moving the logic into the
table, like this for example:

--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -1765,6 +1765,13 @@ project-forget-project
 
 (defvar project--dir-history)
 
+(defun project--project-dir-completion-table (string pred action)
+  (cond
+   ((eq action 'metadata)
+    '(metadata . ((category . project-dir))))
+   (t
+    (complete-with-action action (cons "... (choose a dir)" project--list) string pred))))
+
 (defun project-prompt-project-dir ()
   "Prompt the user for a directory that is one of the known project roots.
 The project is chosen among projects known from the project list,
@@ -1772,18 +1779,14 @@ project-prompt-project-dir
 It's also possible to enter an arbitrary directory not in the list."
   (project--ensure-read-project-list)
   (let* ((dir-choice "... (choose a dir)")
-         (choices
-          ;; XXX: Just using this for the category (for the substring
-          ;; completion style).
-          (project--file-completion-table
-           (append project--list `(,dir-choice))))
          (project--dir-history (project-known-project-roots))
          (pr-dir ""))
     (while (equal pr-dir "")
       ;; If the user simply pressed RET, do this again until they don't.
       (setq pr-dir
             (let (history-add-new-input)
-              (completing-read "Select project: " choices nil t nil 'project--dir-history))))
+              (completing-read "Select project: " #'project--project-dir-completion-table
+                               nil t nil 'project--dir-history))))
     (if (equal pr-dir dir-choice)
         (read-directory-name "Select directory: " default-directory nil t)
       pr-dir)))

The trickier case is when the table actually has some internal state or
calculation which it needs to preserve across multiple calls.  Like I
recently asked about in my recent mail to emacs-devel with subject
"State and caching in completion tables".

If there was a canonical way for a completion table to maintain some
state which *doesn't* require the completion table to be a lambda, I
think most completion tables could become defuns instead of lambdas.
(And then they could be customized based on the function symbol)