unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: emacs-devel@gnu.org
Subject: Re: Documentation about the completion framework
Date: Mon, 21 Jan 2019 16:17:11 -0500	[thread overview]
Message-ID: <jwvef95btxe.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: 62c8a43a-16f4-cd86-da24-df3dcbe68537@grinta.net

> Unfortunately docstring of the relevant elisp functions are not always
> detailed and in some cases not complete. I would also really appreciate
> some documentation on how the framework can be used and how is it
> supposed to be used, even better if there would be some hints on how to
> keep it efficient.
>
> Does such documentation exist somewhere? Does anyone have any resource
> to recommend?

It's mostly available in docstrings.
For example, C-h o completion-at-point-functions says:

    [...]
    NOTE: These functions should be cheap to run since they’re sometimes
    run from ‘post-command-hook’; and they should ideally only choose
    which kind of completion table to use, and not pre-filter it based
    on the current text between START and END (e.g., they should not
    obey ‘completion-styles’).

> One specific question I have: in some circumstances completion-at-point
> enters a mode in which it is called for each keystroke (I think this is
> completion-in-region-mode). How does this happen and why?  I would like
> to avoid that my function to collect completion targets get called on
> each keystroke. Is there a way to do that?

Yes: don't collect candidates when that function is called.
Instead, the function should return a completion table which will
compute the candidates only if it's called.  E.g.:

    (defun my-completion-at-point-function ()
      (when (relevant)
        (let (candidates
              (candidates-computed nil)
              (start ...)
              (end ...)
              (completion-table
               (lambda (string pred action)
                 (unless candidates-computed
                   (setq candidates-computed t)
                   (setq candidates (my-collect-candidates)))
                 (complete-with-action action candidates string pred))))
          (list start end completion-table))))

tho you probably want to cache your completion candidates elsewhere
(exactly where to cache them depends on when they need to be
recomputed, so often it's best to cache them in a global variable and
flush them from other places in the code).


        Stefan




  parent reply	other threads:[~2019-01-21 21:17 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-21 19:22 Documentation about the completion framework Daniele Nicolodi
2019-01-21 19:49 ` Eric Abrahamsen
2019-01-21 22:33   ` Daniele Nicolodi
2019-01-21 23:11     ` Eric Abrahamsen
2019-01-21 23:13   ` Drew Adams
2019-01-22 16:31     ` Eric Abrahamsen
2019-01-22 20:22       ` Drew Adams
2019-01-21 21:17 ` Stefan Monnier [this message]
2019-01-21 22:21   ` Daniele Nicolodi
2019-01-22  2:02   ` Daniele Nicolodi
2019-01-22  3:37     ` Stefan Monnier
2019-01-22  3:53       ` Daniele Nicolodi
2019-01-22 12:11         ` Stefan Monnier

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=jwvef95btxe.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --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).