+** Company
+#+BEGIN_SRC emacs-lisp
+(use-package company
+ :ensure t
+ :bind (("M-/" . company-complete))
+ :config
+ (setq company-echo-delay 0 ; remove blinking
+ company-show-numbers t ; show numbers for easy selection
+ company-selection-wrap-around t
+ company-require-match nil
+ company-dabbrev-ignore-case t
+ company-dabbrev-ignore-invisible t
+ company-dabbrev-other-buffers t
+ company-dabbrev-downcase nil
+ company-dabbrev-code-everywhere t
+ company-tooltip-align-annotations t
+ company-minimum-prefix-length 1
+ company-global-modes '(not)
+ company-lighter-base "(C)")
+ (global-company-mode 1)
+ :bind (:map company-active-map
+ ("C-n" . company-select-next)
+ ("C-p" . company-select-previous)
+ ("M-?" . company-show-doc-buffer)
+ ("M-." . company-show-location)
+ )
+ )
+#+END_SRC
+*** C/C++
+#+BEGIN_SRC emacs-lisp
+ (use-package company-c-headers
+ :ensure t
+ :config (progn
+ (defun malb/ede-object-system-include-path ()
+ "Return the system include path for the current buffer."
+ (when ede-object
+ (ede-system-include-path ede-object)))
+
+ (setq company-c-headers-path-system
+ #'malb/ede-object-system-include-path)
+
+ (add-to-list 'company-backends #'company-c-headers)))
+#+END_SRC
+*** Python
+#+BEGIN_SRC emacs-lisp
+(use-package company-anaconda
+ :ensure t
+ :config (add-to-list 'company-backends #'company-anaconda))
+#+END_SRC
+*** Perl
+#+BEGIN_SRC emacs-lisp
+ (use-package company-plsense
+ :ensure t
+ )
+#+END_SRC
+*** LaTeX
+#+BEGIN_SRC emacs-lisp
+ (use-package company-math
+ :ensure t)
+#+END_SRC
+#+BEGIN_SRC emacs-lisp
+ (use-package company-auctex
+ :ensure t
+ :config (progn
+ (defun company-auctex-labels (command &optional arg &rest ignored)
+ "company-auctex-labels backend"
+ (interactive (list 'interactive))
+ (case command
+ (interactive (company-begin-backend 'company-auctex-labels))
+ (prefix (company-auctex-prefix "\\\\.*ref{\\([^}]*\\)\\="))
+ (candidates (company-auctex-label-candidates arg))))
+
+ (add-to-list 'company-backends
+ '(company-auctex-macros
+ company-auctex-environments
+ company-math-symbols-unicode
+ company-math-symbols-latex))
+
+ (add-to-list 'company-backends #'company-auctex-labels)
+ (add-to-list 'company-backends #'company-auctex-bibs)
+ )
+ )
+#+END_SRC
+#+BEGIN_SRC emacs-lisp
+ (use-package company-bibtex
+ :ensure t
+ )
+#+END_SRC
+*** Shell
+
+ #+BEGIN_SRC emacs-lisp
+ (use-package company-shell
+ :ensure t
+ :config (progn
+ (setq company-shell-modes '(sh-mode shell-mode))
+ (add-to-list 'company-backends 'company-shell)))
+ #+END_SRC
+
+*** YaSnippet
+
+ Add YasSippet support for all company backends. ([[https://github.com/syl20bnr/spacemacs/pull/179][source]])
+
+ *Note:* Do this at the end of =company-mode= config.
+
+ #+BEGIN_SRC emacs-lisp
+ (defvar malb/company-mode/enable-yas t
+ "Enable yasnippet for all backends.")
+
+ (defun malb/company-mode/backend-with-yas (backend)
+ (if (or (not malb/company-mode/enable-yas)
+ (and (listp backend)
+ (member 'company-yasnippet backend)))
+ backend
+ (append (if (consp backend) backend (list backend))
+ '(:with company-yasnippet))))
+
+ (setq company-backends
+ (mapcar #'malb/company-mode/backend-with-yas company-backends))
+ #+END_SRC
+
+*** All the words
+
+ Enable/disable company completion from ispell dictionaries ([[https://github.com/redguardtoo/emacs.d/blob/master/lisp/init-company.el][source]])
+
+ #+BEGIN_SRC emacs-lisp
+ (defun malb/toggle-company-ispell ()
+ (interactive)
+ (cond
+ ((member '(company-ispell :with company-yasnippet) company-backends)
+ (setq company-backends (delete '(company-ispell :with company-yasnippet) company-backends))
+ (add-to-list 'company-backends '(company-dabbrev :with company-yasnippet) t)
+ (message "company-ispell disabled"))
+ (t
+ (setq company-backends (delete '(company-dabbrev :with company-yasnippet) company-backends))
+ (add-to-list 'company-backends '(company-ispell :with company-yasnippet) t)
+ (message "company-ispell enabled!"))))
+
+ (defun malb/company-ispell-setup ()
+ ;; @see https://github.com/company-mode/company-mode/issues/50
+ (when (boundp 'company-backends)
+ (make-local-variable 'company-backends)
+ (setq company-backends (delete '(company-dabbrev :with company-yasnippet) company-backends))
+ (add-to-list 'company-backends '(company-ispell :with company-yasnippet) t)
+ ;; https://github.com/redguardtoo/emacs.d/issues/473
+ (if (and (boundp 'ispell-alternate-dictionary)
+ ispell-alternate-dictionary)
+ (setq company-ispell-dictionary ispell-alternate-dictionary))))
+ #+END_SRC
+
+*** Tab DWIM
+
+ 1. =yas-expand= is run first and does what it has to, then it calls =malb/indent-fold-or-complete=.
+
+ 2. This function then hopefully does what I want:
+
+ a. if a region is active, just indent
+ b. if we’re looking at a space after a non-whitespace character, we try some company-expansion
+ c. If =hs-minor-mode= or =outline-minor-mode= is active, try those next
+ d. otherwise call whatever would have been called otherwise.
+
+ ([[http://emacs.stackexchange.com/q/21182/8930][source]], [[http://emacs.stackexchange.com/q/7908/8930][source]])
+
+#+BEGIN_SRC emacs-lisp
+ (defun malb/indent-fold-or-complete (&optional arg)
+ (interactive "P")
+ (cond
+ ;; if a region is active, indent
+ ((use-region-p)
+ (indent-region (region-beginning)
+ (region-end)))
+ ;; if the next char is space or eol, but prev char not whitespace
+ ((and (not (active-minibuffer-window))
+ (or (looking-at " ")
+ (looking-at "$"))
+ (looking-back "[^[:space:]]")
+ (not (looking-back "^")))
+
+ (cond (company-mode (company-complete-common))
+ (auto-complete-mode (auto-complete))))
+
+ ;; no whitespace anywhere
+ ((and (not (active-minibuffer-window))
+ (looking-at "[^[:space:]]")
+ (looking-back "[^[:space:]]")
+ (not (looking-back "^")))
+ (cond
+ ((bound-and-true-p hs-minor-mode)
+ (save-excursion (end-of-line) (hs-toggle-hiding)))
+ ((bound-and-true-p outline-minor-mode)
+ (save-excursion (outline-cycle)))))
+
+ ;; by default just call whatever was bound
+ (t
+ (let ((fn (or (lookup-key (current-local-map) (kbd "TAB"))
+ 'indent-for-tab-command)))
+ (if (not (called-interactively-p 'any))
+ (fn arg)
+ (setq this-command fn)
+ (call-interactively fn))))))
+
+ (defun malb/toggle-fold ()
+ (interactive)
+ (cond ((eq major-mode 'org-mode)
+ (org-force-cycle-archived))
+ ((bound-and-true-p hs-minor-mode)
+ (save-excursion
+ (end-of-line)
+ (hs-toggle-hiding)))
+
+ ((bound-and-true-p outline-minor-mode)
+ (save-excursion
+ (outline-cycle)))))
+
+ (bind-key "<tab>" #'malb/indent-fold-or-complete)
+ (bind-key "C-<tab>" #'malb/toggle-fold)
+#+END_SRC