Fix set-company-backends! & update docstring

+ It wasn't preserving insertion order of multiple backends
+ It failed when BACKENDS = nil (supposed to unset mode backends)
+ Use eq/equal as a test-fn conditionally (glorious, glorious premature
  optimization)
This commit is contained in:
Henrik Lissner 2018-06-20 19:41:49 +02:00
parent 898449e374
commit aa4c9744f8
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -2,20 +2,34 @@
;;;###autodef ;;;###autodef
(defun set-company-backend! (modes &rest backends) (defun set-company-backend! (modes &rest backends)
"Prepends BACKENDS to `company-backends' in major MODES. "Prepends BACKENDS (in order) to `company-backends' in MODES.
MODES should be one major-mode symbol or a list of them." MODES should be one symbol or a list of them, representing major or minor modes.
This will overwrite backends for MODES on consecutive uses.
If BACKENDS is just 'nil, unset the backends for MODES.
Examples:
(set-company-backend! 'js2-mode 'company-tide 'company-yasnippet)
(set-company-backend! 'sh-mode
'(company-shell :with company-yasnippet))
(set-company-backend! 'js2-mode
'(:separate company-irony-c-headers company-irony))
(set-company-backend! 'sh-mode nil)"
(dolist (mode (doom-enlist modes)) (dolist (mode (doom-enlist modes))
(let ((fn (intern (format "+company|init-%s" mode))) (let ((fn (intern (format "+company|init-%s" mode)))
(hook (intern (format "%s-hook" mode)))) (hook (intern (format "%s-hook" mode))))
(cond (backends (cond ((and backends (not (eq (car backends) 'nil)))
(fset fn (fset fn
(lambda () (when (or (eq major-mode mode) (lambda ()
(when (or (eq major-mode mode)
(and (boundp mode) (symbol-value mode))) (and (boundp mode) (symbol-value mode)))
(require 'company) (require 'company)
(make-local-variable 'company-backends) (make-local-variable 'company-backends)
(dolist (backend backends) (dolist (backend (reverse backends))
(cl-pushnew backend company-backends :test #'equal))))) (cl-pushnew backend company-backends
:test (if (symbolp backend) #'eq #'equal))))))
(add-hook hook fn)) (add-hook hook fn))
(t (t
(fmakunbound fn) (fmakunbound fn)