Skip to content

Instantly share code, notes, and snippets.

@aaronjensen
Last active May 25, 2021 21:11
Show Gist options
  • Save aaronjensen/a46f88dbd1ab9bb3aa22 to your computer and use it in GitHub Desktop.
Save aaronjensen/a46f88dbd1ab9bb3aa22 to your computer and use it in GitHub Desktop.
Enables tab to complete and cycle completions with company-mode, similar to neocomplete in vim
(defun company-complete-cycle-enable ()
"Enables TAB and S-tab to cycle completions without requiring
return to confirm"
(with-eval-after-load 'company
(define-key company-active-map [tab] 'company-complete-cycle-next)
(define-key company-active-map (kbd "TAB") 'company-complete-cycle-next)
(define-key company-active-map (kbd "<S-tab>") 'company-complete-cycle-previous)
(define-key company-active-map (kbd "RET") nil)
(define-key company-active-map (kbd "<return>") nil)
(put 'company-complete-cycle-next 'company-keep t)
(put 'company-complete-cycle-previous 'company-keep t)
(ad-activate 'company--create-lines)
(ad-activate 'company-pseudo-tooltip-frontend)
(add-to-list 'company-transformers 'company-add-no-selection-placeholder t)))
(defvar company-no-selection-placeholder "**company-no-selection**")
(defvar company-before-complete-point nil)
(defun company-complete-cycle-next (&optional arg)
(interactive "p")
(company-select-next arg)
(company-complete-selection-and-stay))
(defun company-complete-cycle-previous (&optional arg)
(interactive "p")
(company-select-previous arg)
(company-complete-selection-and-stay))
(defun company-complete-selection-and-stay ()
(when (company-manual-begin)
(let ((result (nth company-selection company-candidates)))
(when company-before-complete-point
(message "deleting")
(delete-region company-before-complete-point (point)))
(setq company-before-complete-point (point))
(unless (eq result company-no-selection-placeholder)
(company--insert-candidate result))
(company-call-frontends 'update)
(company-call-frontends 'post-command))))
(defadvice company--create-lines (after remove-no-selection ())
"Remove the company no selection placeholder"
(let ((first (car ad-return-value)))
(when (and first
(equal company-no-selection-placeholder
(string-trim (substring-no-properties first))))
(setq ad-return-value (cdr ad-return-value)))))
(defadvice company-pseudo-tooltip-frontend (before reset-before-complete-point (command))
(when (equal command 'hide)
(setq company-before-complete-point nil)))
(defun company-add-no-selection-placeholder (candidates)
(if (> (length candidates) 1)
(push company-no-selection-placeholder candidates)
candidates))
@aaronjensen
Copy link
Author

@kassick
Copy link

kassick commented Jun 3, 2016

Hey aaron, I did some modifications to your code to enable selecting options with arguments templates rather then just completing them (C-return and M-)
https://gist.github.com/kassick/845e4e59662a5b5c47f39bfd5941d31f

@nikital
Copy link

nikital commented Apr 22, 2017

Hello @aaronjensen, thanks for the very useful snippet!
One problem I had is broken repeat functionality (dot) with evil-mode. To fix it:

(evil-declare-change-repeat 'company-simple-complete-next)
(evil-declare-change-repeat 'company-simple-complete-previous)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment