emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

dynamic-completion-table


From: Roland Winkler
Subject: dynamic-completion-table
Date: Thu, 19 Jun 2003 16:31:46 +0200

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 address@hidden





reply via email to

[Prev in Thread] Current Thread [Next in Thread]