diff --git a/contrib/lang/c-c++/packages.el b/contrib/lang/c-c++/packages.el index 236b08bf2..7e9f3bf43 100644 --- a/contrib/lang/c-c++/packages.el +++ b/contrib/lang/c-c++/packages.el @@ -50,7 +50,7 @@ which require an initialization must be listed explicitly in the list.") :mode (("CMakeLists\\.txt\\'" . cmake-mode) ("\\.cmake\\'" . cmake-mode)) :init (progn - (spacemacs|add-mode-company-backend cmake-mode company-cmake)))) + (spacemacs|add-local-company-backend cmake-mode company-cmake)))) (defun c-c++/init-company () ;; .clang_complete file loading @@ -102,8 +102,8 @@ which require an initialization must be listed explicitly in the list.") :defer t :init (progn - (spacemacs|add-mode-company-backend c-mode company-c-headers) - (spacemacs|add-mode-company-backend c++-mode company-c-headers)))) + (spacemacs|add-local-company-backend c-mode company-c-headers) + (spacemacs|add-local-company-backend c++-mode company-c-headers)))) (defun c-c++/init-flycheck () (add-to-hooks 'flycheck-mode '(c-mode-hook c++-mode-hook))) diff --git a/contrib/lang/csharp/packages.el b/contrib/lang/csharp/packages.el index d0ef6a22e..7b63c66d9 100644 --- a/contrib/lang/csharp/packages.el +++ b/contrib/lang/csharp/packages.el @@ -60,4 +60,4 @@ "mi" 'omnisharp-fix-usings "m=" 'omnisharp-code-format - (spacemacs|add-mode-company-backend csharp-mode company-omnisharp))) + (spacemacs|add-local-company-backend csharp-mode company-omnisharp))) diff --git a/contrib/lang/ess/packages.el b/contrib/lang/ess/packages.el index f1fa02d1e..b67610377 100644 --- a/contrib/lang/ess/packages.el +++ b/contrib/lang/ess/packages.el @@ -147,4 +147,4 @@ not play nicely with autoloads" :defer t :init (progn - (spacemacs|add-mode-company-backend ess-mode company-ess-backend)))) + (spacemacs|add-local-company-backend ess-mode company-ess-backend)))) diff --git a/contrib/lang/go/packages.el b/contrib/lang/go/packages.el index c41fb7e70..0fabe7388 100644 --- a/contrib/lang/go/packages.el +++ b/contrib/lang/go/packages.el @@ -45,4 +45,4 @@ which require an initialization must be listed explicitly in the list.") :defer t :init (progn - (spacemacs|add-mode-company-backend go-mode company-go)))) + (spacemacs|add-local-company-backend go-mode company-go)))) diff --git a/contrib/lang/haskell/packages.el b/contrib/lang/haskell/packages.el index f523ce2eb..111752c1d 100644 --- a/contrib/lang/haskell/packages.el +++ b/contrib/lang/haskell/packages.el @@ -232,7 +232,7 @@ :defer t :init (progn - (spacemacs|add-mode-company-backend haskell-mode company-ghc)))) + (spacemacs|add-local-company-backend haskell-mode company-ghc)))) (defun haskell/init-hi2 () (use-package hi2 diff --git a/contrib/lang/javascript/packages.el b/contrib/lang/javascript/packages.el index b1b0bb7d3..e331d8021 100644 --- a/contrib/lang/javascript/packages.el +++ b/contrib/lang/javascript/packages.el @@ -169,4 +169,4 @@ which require an initialization must be listed explicitly in the list.") :defer t :init (progn - (spacemacs|add-mode-company-backend tern-mode company-tern)))) + (spacemacs|add-local-company-backend tern-mode company-tern)))) diff --git a/contrib/lang/python/packages.el b/contrib/lang/python/packages.el index 25646f255..a9d3c68eb 100644 --- a/contrib/lang/python/packages.el +++ b/contrib/lang/python/packages.el @@ -60,7 +60,7 @@ which require an initialization must be listed explicitly in the list.") :defer t :init (progn - (spacemacs|add-mode-company-backend python-mode company-anaconda)))) + (spacemacs|add-local-company-backend python-mode company-anaconda)))) (defun python/init-cython-mode () (use-package cython-mode diff --git a/contrib/ycmd/packages.el b/contrib/ycmd/packages.el index 9b2869119..2799992cd 100644 --- a/contrib/ycmd/packages.el +++ b/contrib/ycmd/packages.el @@ -32,8 +32,8 @@ which require an initialization must be listed explicitly in the list.") :defer t :init (progn - (spacemacs|add-mode-company-backend c-mode company-ycmd) - (spacemacs|add-mode-company-backend c++-mode company-ycmd)))) + (spacemacs|add-local-company-backend c-mode company-ycmd) + (spacemacs|add-local-company-backend c++-mode company-ycmd)))) (defun ycmd/init-flycheck-ycmd () (use-package flycheck-ycmd diff --git a/spacemacs/funcs.el b/spacemacs/funcs.el index 8867ef1ac..74f51c725 100644 --- a/spacemacs/funcs.el +++ b/spacemacs/funcs.el @@ -777,25 +777,26 @@ If ASCII si not provided then UNICODE is used instead." (let ((comint-buffer-maximum-size 0)) (comint-truncate-buffer))) -(defmacro spacemacs|reset-local-company-backends (mode) - "Helper to make `company-backends' buffer local and reset it. -Use *only* if a default backend interferes with completion in a specific mode." - `(add-hook ',(intern (format "%S-hook" mode)) - (lambda () - (set (make-variable-buffer-local 'company-backends) nil)))) - -(defun spacemacs//make-company-backends-local () +(defun spacemacs//make-company-backends-buffer-local () "Helper to make `company-backends' buffer local and reset it." - (make-variable-buffer-local 'company-backends)) + (set (make-variable-buffer-local 'company-backends) nil)) -(defmacro spacemacs|add-mode-company-backend (mode backend) - "Add a new `company-mode' backend for a specific mode." - (let ((back-hook-sym (intern (format "spacemacs//add-%S-%S" mode backend))) - (mode-hook-sym (intern (format "%S-hook" mode)))) - ; Need the company *layer* for the backend-with-yas function, not just the package. See #961 +(defmacro spacemacs|add-local-company-backend (mode backend &optional with-yas) + "Helper macro to add local `company-mode' BACKEND for MODE. + +If WITH-YAS is non nil then the the `company-yasnippet' is consed to BACKEND." + (let ((mode-hook (intern (format "%S-hook" mode))) + (add-backend (intern (format "spacemacs//%S-add-%S-backend" + mode backend))) + (backend2 (if with-yas + `(spacemacs/company-backend-with-yas ',backend) + `(quote ,backend)))) `(when (configuration-layer/layer-declaredp 'company-mode) - ;; this hook will be added once even if this macro is used multiple times on the same mode. - (add-hook ',mode-hook-sym 'spacemacs//make-company-backends-local) - (defun ,back-hook-sym () - (add-to-list 'company-backends (spacemacs/company-backend-with-yas ',backend))) - (add-hook ',mode-hook-sym ',back-hook-sym t)))) + (add-hook ',mode-hook + 'spacemacs//make-company-backends-buffer-local) + (defun ,add-backend () + ,(format "Add %S backend to %S" backend mode) + (add-to-list 'company-backends ,backend2)) + ;; important to append this function to the hook in order to + ;; execute it at the end + (add-hook ',mode-hook ',add-backend t))))