From cb43a91a15f40d9778372be0817bb9442630e1f8 Mon Sep 17 00:00:00 2001 From: Tristan Hume Date: Wed, 25 Mar 2015 21:07:42 -0400 Subject: [PATCH] Add company backend add macro and use it everywhere --- contrib/lang/c-c++/packages.el | 18 +++--------------- contrib/lang/csharp/packages.el | 5 +---- contrib/lang/ess/packages.el | 7 +------ contrib/lang/go/packages.el | 6 +----- contrib/lang/haskell/packages.el | 7 +------ contrib/lang/javascript/packages.el | 8 +------- contrib/lang/python/packages.el | 7 +------ contrib/ycmd/packages.el | 5 ++++- spacemacs/funcs.el | 19 ++++++++++++++++++- 9 files changed, 31 insertions(+), 51 deletions(-) diff --git a/contrib/lang/c-c++/packages.el b/contrib/lang/c-c++/packages.el index fdace0437..236b08bf2 100644 --- a/contrib/lang/c-c++/packages.el +++ b/contrib/lang/c-c++/packages.el @@ -50,12 +50,7 @@ which require an initialization must be listed explicitly in the list.") :mode (("CMakeLists\\.txt\\'" . cmake-mode) ("\\.cmake\\'" . cmake-mode)) :init (progn - (spacemacs|reset-local-company-backends cmake-mode) - (defun spacemacs//cmake-company-backend () - "Add cmake company backend." - (push (spacemacs/company-backend-with-yas 'company-cmake) - company-backends)) - (add-hook 'cmake-mode-hook 'spacemacs//cmake-company-backend t)))) + (spacemacs|add-mode-company-backend cmake-mode company-cmake)))) (defun c-c++/init-company () ;; .clang_complete file loading @@ -107,14 +102,8 @@ which require an initialization must be listed explicitly in the list.") :defer t :init (progn - (spacemacs|reset-local-company-backends c-mode) - (spacemacs|reset-local-company-backends c++-mode) - (defun spacemacs//c-headers-company-backend () - "Add c-headers company backend." - (push (spacemacs/company-backend-with-yas 'company-c-headers) - company-backends)) - (add-hook 'c-mode-hook 'spacemacs//c-headers-company-backend t) - (add-hook 'c++-mode-hook 'spacemacs//c-headers-company-backend t)))) + (spacemacs|add-mode-company-backend c-mode company-c-headers) + (spacemacs|add-mode-company-backend c++-mode company-c-headers)))) (defun c-c++/init-flycheck () (add-to-hooks 'flycheck-mode '(c-mode-hook c++-mode-hook))) @@ -139,4 +128,3 @@ which require an initialization must be listed explicitly in the list.") (require 'stickyfunc-enhance)) (add-to-hooks 'spacemacs/lazy-load-stickyfunc-enhance '(c-mode-hook c++-mode-hook))))) - diff --git a/contrib/lang/csharp/packages.el b/contrib/lang/csharp/packages.el index 83bb897e6..d0ef6a22e 100644 --- a/contrib/lang/csharp/packages.el +++ b/contrib/lang/csharp/packages.el @@ -60,7 +60,4 @@ "mi" 'omnisharp-fix-usings "m=" 'omnisharp-code-format - ;; Init omnisharp company backend - (eval-after-load 'company - '(add-to-list 'company-backends 'company-omnisharp)) - )) + (spacemacs|add-mode-company-backend csharp-mode company-omnisharp))) diff --git a/contrib/lang/ess/packages.el b/contrib/lang/ess/packages.el index ec5d5aa81..f1fa02d1e 100644 --- a/contrib/lang/ess/packages.el +++ b/contrib/lang/ess/packages.el @@ -147,9 +147,4 @@ not play nicely with autoloads" :defer t :init (progn - (spacemacs|reset-local-company-backends ess-mode) - (defun spacemacs//ess-company-backend () - "Add ESS company backend." - (push (spacemacs/company-backend-with-yas 'company-ess-backend) - company-backends)) - (add-hook 'ess-mode-hook 'spacemacs//ess-company-backend t)))) + (spacemacs|add-mode-company-backend ess-mode company-ess-backend)))) diff --git a/contrib/lang/go/packages.el b/contrib/lang/go/packages.el index f7cb6cc69..c41fb7e70 100644 --- a/contrib/lang/go/packages.el +++ b/contrib/lang/go/packages.el @@ -45,8 +45,4 @@ which require an initialization must be listed explicitly in the list.") :defer t :init (progn - (spacemacs|reset-local-company-backends go-mode) - (defun spacemacs//go-company-backend () - (push (spacemacs/company-backend-with-yas 'company-go) - company-backends)) - (add-hook 'go-mode-hook 'spacemacs//go-company-backend t)))) + (spacemacs|add-mode-company-backend go-mode company-go)))) diff --git a/contrib/lang/haskell/packages.el b/contrib/lang/haskell/packages.el index a482f550f..f523ce2eb 100644 --- a/contrib/lang/haskell/packages.el +++ b/contrib/lang/haskell/packages.el @@ -232,12 +232,7 @@ :defer t :init (progn - (spacemacs|reset-local-company-backends haskell-mode) - (defun spacemacs//ghc-company-backend () - "Add GHC company backend." - (push (spacemacs/company-backend-with-yas 'company-ghc) - company-backends)) - (add-hook 'haskell-mode-hook 'spacemacs//ghc-company-backend t)))) + (spacemacs|add-mode-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 43ec5ad8b..b1b0bb7d3 100644 --- a/contrib/lang/javascript/packages.el +++ b/contrib/lang/javascript/packages.el @@ -169,10 +169,4 @@ which require an initialization must be listed explicitly in the list.") :defer t :init (progn - (spacemacs|reset-local-company-backends tern-mode) - (defun spacemacs//tern-company-backend () - "Add tern company backend." - (push (spacemacs/company-backend-with-yas 'company-tern) - company-backends)) - (add-hook 'tern-mode-hook 'spacemacs//tern-company-backend t)))) - + (spacemacs|add-mode-company-backend tern-mode company-tern)))) diff --git a/contrib/lang/python/packages.el b/contrib/lang/python/packages.el index 26a92e739..25646f255 100644 --- a/contrib/lang/python/packages.el +++ b/contrib/lang/python/packages.el @@ -60,12 +60,7 @@ which require an initialization must be listed explicitly in the list.") :defer t :init (progn - (spacemacs|reset-local-company-backends python-mode) - (defun spacemacs//anaconda-company-backend () - "Add anaconda company backend." - (push (spacemacs/company-backend-with-yas 'company-anaconda) - company-backends)) - (add-hook 'python-mode-hook 'spacemacs//anaconda-company-backend t)))) + (spacemacs|add-mode-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 3c9a24c2f..9b2869119 100644 --- a/contrib/ycmd/packages.el +++ b/contrib/ycmd/packages.el @@ -30,7 +30,10 @@ which require an initialization must be listed explicitly in the list.") (defun ycmd/init-company-ycmd () (use-package company-ycmd :defer t - :init (add-hook 'ycmd-mode-hook 'company-ycmd-setup))) + :init + (progn + (spacemacs|add-mode-company-backend c-mode company-ycmd) + (spacemacs|add-mode-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 b7b399d57..8867ef1ac 100644 --- a/spacemacs/funcs.el +++ b/spacemacs/funcs.el @@ -778,7 +778,24 @@ If ASCII si not provided then UNICODE is used instead." (comint-truncate-buffer))) (defmacro spacemacs|reset-local-company-backends (mode) - "Helper to make `company-backends' buffer local and reset it." + "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 () + "Helper to make `company-backends' buffer local and reset it." + (make-variable-buffer-local 'company-backends)) + +(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 + `(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))))