2

I wrote a backend for COMPANY.

;;----------------COMPANY BACKEND-----------------------------

(defvar oef-mode-completions '()
  "The content of this variable is generated automatically for COMPANY completion."
  )

(setq oef-mode-completions
      (append  oef-answers-options
           oef-definitions-commands
           oef-commands
           oef-definitions-special-commands
           oef-doc-commands
           oef-storage-types
           oef-menu-exo-init-types
           oef-menu-doc-init-types
           oef-defined-variables
           oef-comparison-operators
           oef-language-reserved-words
           oef-definitions-wims-functions
           oef-wims-functions
           oef-definitions-slib-scripts
           oef-slib-scripts
           oef-pari-functions
           oef-maths-functions
           oef-random-functions
           oef-canvasdraw-commands))

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

  (case command
        (interactive (company-begin-backend 'company-oef-mode-backend))
        (prefix
         (and (eq major-mode 'oef-mode)
              (company-grab-word))
         )
        (candidates
         (remove-if-not
          (lambda (c) (string-prefix-p arg c))
          oef-mode-completions))
;;      (meta (format "This value is named %s" arg))
        )
  )

(add-to-list 'company-backends 'company-oef-mode-backend)

It's working but it doesn't give the control to the next backend. If I look at the documentation C-h v company-backends I read :

‘prefix’: The backend should return the text to be completed. It must be text immediately before point. Returning nil from this command passes control to the next backend.

Until now my code return the word or an empty string that's why the next backend is ignored.

  • Note: if you write a completion-at-point-function(s), it will work with Company as well as with the built-in completion facilities. – Stefan Oct 28 '18 at 13:59
1

If found this solution :

In the prefix part, the 3 letters before the point are compared with a list of 3 letters (I called this list oef-candidats). If there is a match, the candidates part make a popup suggestions. If not, the prefix part return nil so the next backend is called automatically.

;;----------------COMPANY BACKEND-----------------------------

(defvar oef-completions '()
  "The content of this variable is a list of keyphrases (keywords...).  It's generated automatically for COMPANY completion." 
  )

(defvar oef-grabed-word nil
  "The content of this variable is get before point by company-mode."
  )

(defvar oef-candidats nil
  "List of candidats.  Made of Three first letters of items in oef-completion."
  )

(setq oef-completions
      (append  oef-answers-options
           oef-definitions-commands
           oef-commands
           oef-definitions-special-commands
           oef-doc-commands
           oef-storage-types
           oef-menu-exo-init-types
           oef-menu-doc-init-types
           oef-defined-variables
           oef-comparison-operators
           oef-language-reserved-words
           oef-definitions-wims-functions
           oef-wims-functions
           oef-definitions-slib-scripts
           oef-slib-scripts
           oef-pari-functions
           oef-maths-functions
           oef-random-functions
           oef-canvasdraw-commands))

(defun oef-make-candidats()
  "Make a list of candidats (3 first characters) to detect if oef-mode-backend is required."
  (interactive)
  (let ((mylist oef-completions)(myword))
    (while mylist
      (setq myword (pop mylist))
      (if (> (length myword) 2)
      (add-to-list 'oef-candidats (substring myword 0 3))
    ))))

(defun company-oef-mode-backend (command &optional arg &rest ignored)
  "Detect if company-oef-mode-bakend is required.  If yes company-oef-mode-backend will sugest competions.  If not the next backend is called."
  (interactive (list 'interactive))
  (case command
    (interactive (company-begin-backend 'company-oef-mode-backend))
    (prefix
     (setq oef-grabed-word (company-grab-word))
     (if (and (eq major-mode 'oef-mode)
          (member
           oef-grabed-word
           oef-candidats
           ))
     oef-grabed-word
       nil)
     )      
    (candidates
     (remove-if-not
      (lambda (c) (string-prefix-p arg c))
      oef-completions))
    )
  )

(add-to-list 'company-backends 'company-oef-mode-backend)

;;-----------MAJOR MODE----------------------------------------
;;;###autoload
(define-derived-mode oef-mode sgml-mode
  "oef-mode"
  "'Online Exercise Format' mode"

  (oef-make-candidats) ; candidats for completions with company-oef-mode-backend
   )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.