Skip to content

Instantly share code, notes, and snippets.

@yuttie
Forked from aaronjensen/company-complete-cycle.el
Last active September 3, 2016 12:28
Show Gist options
  • Select an option

  • Save yuttie/63201d77a8971f87a731f9a35463d712 to your computer and use it in GitHub Desktop.

Select an option

Save yuttie/63201d77a8971f87a731f9a35463d712 to your computer and use it in GitHub Desktop.
Enables tab to complete and cycle completions with company-mode, similar to neocomplete in vim
;;; company-simple-complete.el --- Vim-like completion style
;; Package-Requires: ((company))
;;; Commentary:
;; Modify company so that tab and S-tab cycle through completions without
;; needing to hit enter.
;;; Code:
(defvar-local company-simple-complete--previous-prefix nil)
(defvar-local company-simple-complete--before-complete-point nil)
(defun company-simple-complete-frontend (command)
(when (or (eq command 'show)
(and (eq command 'update)
(not (equal company-prefix company-simple-complete--previous-prefix))))
(setq company-selection -1
company-simple-complete--previous-prefix company-prefix
company-simple-complete--before-complete-point nil)))
(defun company-simple-complete-next (&optional arg)
(interactive "p")
(company-select-next arg)
(company-simple-complete--complete-selection-and-stay))
(defun company-simple-complete-previous (&optional arg)
(interactive "p")
(company-select-previous arg)
(company-simple-complete--complete-selection-and-stay))
(defun company-simple-complete--complete-selection-and-stay ()
(if (cdr company-candidates)
(when (company-manual-begin)
(when company-simple-complete--before-complete-point
(delete-region company-simple-complete--before-complete-point (point)))
(setq company-simple-complete--before-complete-point (point))
(unless (eq company-selection -1)
(company--insert-candidate (nth company-selection company-candidates)))
(company-call-frontends 'update)
(company-call-frontends 'post-command))
(company-complete-selection)))
(defadvice company-set-selection (around allow-no-selection (selection &optional force-update))
"Allow selection to be -1"
(setq selection
;; TODO deal w/ wrap-around
(if company-selection-wrap-around
(mod selection company-candidates-length)
(max -1 (min (1- company-candidates-length) selection))))
(when (or force-update (not (equal selection company-selection)))
(setq company-selection selection
company-selection-changed t)
(company-call-frontends 'update)))
(defadvice company-tooltip--lines-update-offset (before allow-no-selection (selection _num-lines _limit))
"Allow selection to be -1"
(when (eq selection -1)
(ad-set-arg 0 0)))
(defadvice company-tooltip--simple-update-offset (before allow-no-selection (selection _num-lines limit))
"Allow selection to be -1"
(when (eq selection -1)
(ad-set-arg 0 0)))
(with-eval-after-load 'company
(put 'company-simple-complete-next 'company-keep t)
(put 'company-simple-complete-previous 'company-keep t)
(ad-activate 'company-set-selection)
(ad-activate 'company-tooltip--simple-update-offset)
(ad-activate 'company-tooltip--lines-update-offset)
(setq company-require-match nil)
(add-to-list 'company-frontends 'company-simple-complete-frontend)
(substitute-key-definition 'company-select-next 'company-simple-complete-next company-active-map)
(substitute-key-definition 'company-select-previous 'company-simple-complete-previous company-active-map)
(define-key company-active-map (kbd "RET") nil)
(define-key company-active-map (kbd "<return>") nil))
(provide 'company-simple-complete)
;;; company-simple-complete.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment