I have written code that returns lists of symbols representing modules and exported functions of a programming language. How do I integrate it with some autocompletion in Emacs?

I looked at company and completion-at-point-functions. I managed to run simple example successfully, and then I realised that all these examples use a predefined list of completion candidates.

Last code sample on the following page is a good example http://sixty-north.com/blog/writing-the-simplest-emacs-company-mode-backend

I do not know what the completion candidates will be at this point. How do I create a dynamic list of candidates generated when I run M-x company-sample-backend?

Actually, the example at sixty-north.com can be easily modified to use dynamic completion. The problem lies elsewhere.

Replacing constant

(defconst sample-completions
  '("alan" "john" "ada" "don"))

with a function returning strings with random content and calling that function at the bottom of the backend function works.

In the end I have settled on following code:

(require 'company)

(defun company-lfe-backend (command &optional arg &rest ignored)
  (interactive (list 'interactive))

  (case command
    (interactive (company-begin-backend 'company-lfe-backend))
    ;; unfinished code related to major modes
    (prefix (and (or t
                     (eq major-mode 'fundamental-mode))
     (-map 'symbol-name
            ;; following function returns the structure containing possible
            ;; auto completions

(add-to-list 'company-backends 'company-lfe-backend)
  • I looked at helm documentation, and I found an example that can generate a list of options and print it in the Messages buffer. However, I can't find a way to put it in the buffer I want. Commented Jan 8, 2017 at 22:11
  • Try looking at autocomplete and company.
    – Dan
    Commented Jan 8, 2017 at 22:13
  • github.com/company-mode/company-mode/wiki/Writing-backends the simple backend looks promising Commented Jan 8, 2017 at 23:23
  • This is another interesting tutorial, sixty-north.com/blog/… Commented Jan 8, 2017 at 23:39
  • You can also locally bind completion-at-point-functions to custom functions to extend Emacs' built-in completion.
    – Qudit
    Commented Jan 8, 2017 at 23:40


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.