From: Roland Winkler <roland.winkler@physik.uni-erlangen.de>
Cc: emacs-devel@gnu.org
Subject: Re: dynamic-completion-table
Date: Tue, 24 Jun 2003 16:11:30 +0200 [thread overview]
Message-ID: <16120.23570.881095.34919@tfkp07.physik.uni-erlangen.de> (raw)
In-Reply-To: <E19TMbd-0007yz-Gl@fencepost.gnu.org>
On Fri Jun 20 2003 Richard Stallman wrote:
> "Turn a function FUN returning a completion table, into a completion table.
>
> That sentence is very confusing--you need to say it a different
> way.
I suggest the following. I hope that the new docstring is less
confusing.
Note also that I modified the code: FUN is called with one argument,
the string for which completion is required. Furthermore, in order
to avoid name conflicts make-symbol is used to create local
variables.
(defmacro dynamic-completion-table (fun)
"Use function FUN as a dynamic completion table.
FUN is called with one argument, the string for which completion is required,
and it should return an alist containing all the intended possible
completions. If completion is performed in the minibuffer, FUN will be called
in the buffer from which the minibuffer was entered.
`dynamic-completion-table' then computes the completion, see Info
node `(elisp)Programmed Completion'."
;; (declare (debug t))
(let ((win (make-symbol "window"))
(string (make-symbol "string"))
(predicate (make-symbol "predicate"))
(mode (make-symbol "mode")))
`(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 ,string) ,predicate))
((not ,mode) (try-completion ,string (,fun ,string) ,predicate))
(t (test-completion ,string (,fun ,string) ,predicate)))))))
> 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.
>
> Should this macro produce the code to do that?
> It might be more useful that way.
I think it is conceptually cleaner to create lazy completion tables
by means of a second macro that uses dynamic-completion-table. My
suggestion is the following:
(defmacro lazy-completion-table (var fun &rest args)
"Initialize variable VAR as a lazy completion table.
If the completion table VAR is used for the first time (e.g., by passing VAR
as an argument to `try-completion'), the function FUN is called with arguments
ARGS. FUN must return the completion table that will be stored in VAR. If
completion is requested in the minibuffer, FUN will be called in the buffer
from which the minibuffer was entered. The return value of
`lazy-completion-table' must be used to initialize the value of VAR."
(let ((str (make-symbol "string")))
`(dynamic-completion-table
(lambda (,str)
(unless (listp ,var)
(setq ,var (funcall ',fun ,@args)))
,var))))
Then a typical application of lazy-completion-table is the following:
(defvar bibtex-reference-keys
(lazy-completion-table bibtex-reference-keys bibtex-parse-keys nil nil t)
"Completion table for BibTeX reference keys.")
(make-variable-buffer-local 'bibtex-reference-keys)
Is this code useful for other packages, too so that the above macros
should be made available, e.g., in simple.el?
Suggestions welcome!
Roland
next prev parent reply other threads:[~2003-06-24 14:11 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-06-19 14:31 dynamic-completion-table Roland Winkler
2003-06-20 14:11 ` dynamic-completion-table Richard Stallman
2003-06-24 14:11 ` Roland Winkler [this message]
2003-06-25 19:53 ` dynamic-completion-table Richard Stallman
2003-06-26 13:17 ` dynamic-completion-table Roland Winkler
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=16120.23570.881095.34919@tfkp07.physik.uni-erlangen.de \
--to=roland.winkler@physik.uni-erlangen.de \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).