From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Roland Winkler Newsgroups: gmane.emacs.devel Subject: dynamic-completion-table Date: Thu, 19 Jun 2003 16:31:46 +0200 Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: <16113.51538.619271.508717@tfkp07.physik.uni-erlangen.de> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: main.gmane.org 1056033231 5749 80.91.224.249 (19 Jun 2003 14:33:51 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Thu, 19 Jun 2003 14:33:51 +0000 (UTC) Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Thu Jun 19 16:33:50 2003 Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 19T0U2-0001Ub-00 for ; Thu, 19 Jun 2003 16:33:50 +0200 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.12 #1 (Debian)) id 19T0VB-0005Xj-00 for ; Thu, 19 Jun 2003 16:35:01 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.20) id 19T0Ss-0001g2-O6 for emacs-devel@quimby.gnus.org; Thu, 19 Jun 2003 10:32:38 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.20) id 19T0SP-0001Mo-FR for emacs-devel@gnu.org; Thu, 19 Jun 2003 10:32:09 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.20) id 19T0SN-0001Il-Lu for emacs-devel@gnu.org; Thu, 19 Jun 2003 10:32:08 -0400 Original-Received: from max6.rrze.uni-erlangen.de ([131.188.3.214]) by monty-python.gnu.org with esmtp (Exim 4.20) id 19T0S7-0000hg-VJ for emacs-devel@gnu.org; Thu, 19 Jun 2003 10:31:52 -0400 Original-Received: from tfkp07.physik.uni-erlangen.de by max6.rrze.uni-erlangen.de with ESMTP for emacs-devel@gnu.org; Thu, 19 Jun 2003 16:31:50 +0200 Original-To: emacs-devel@gnu.org X-Mailer: VM 6.96 under Emacs 21.2.1 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Emacs development discussions. List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:15164 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:15164 In order to avoid duplicating code in bibtex.el Stefan Monnier suggested to me to use a macro dynamic-completion-table. (defmacro dynamic-completion-table (fun) "Turn a function FUN returning a completion table, into a completion table. FUN is called with no argument and should return a completion table. If completion is performed in the minibuffer, FUN will be called in the buffer from which the minibuffer was entered." ;; (declare (debug t)) `(lambda (string predicate mode) (with-current-buffer (let ((win (minibuffer-selected-window))) (if (window-live-p win) (window-buffer win) (current-buffer))) (cond ((eq mode t) (all-completions string (,fun) predicate)) ((not mode) (try-completion string (,fun) predicate)) (t (test-completion string (,fun) predicate)))))) I'd like to suggest that it could be made available in, e.g., simple.el. I think it comes quite handy if a completion table must be calculated dynamically by means of an appropriate function. For example, the function sh-add-completer in sh-script.el could use dynamic-completion-table. If completion is performed in the minibuffer, FUN will be called in the buffer from which the minibuffer was entered. I believe that such a "well-defined environment" for evaluating FUN is in general more useful than a macro without it. An alternative to using minibuffer-selected-window might be that the buffer name for evaluating FUN is passed to dynamic-completion-table as an (optional) argument or via a variable like sh-add-buffer in sh-script.el. However, for my application it is an important part of dynamic-completion-table that the buffer name is determined automatically: I use dynamic-completion-table to define the global initial values of buffer-local variables that contain completion lists. The idea is that if the user hits tab, FUN calculates the completion list and stores it in the buffer-local variables. In that way, the buffer-local values of the completion lists are calculated only once - provided the completion lists are required at all. (Maybe, this scheme for initializing completion lists is of interest elsewhere, too?) Maybe, the macro dynamic-completion-table requires some adjustment in order to make it even more useful for other purposes, too. Suggestions welcome! Roland PS: I have not subscribed to emacs-devel@gnu.org