Skip to content

Instantly share code, notes, and snippets.

@sickHamm
Last active June 28, 2016 03:11
Show Gist options
  • Save sickHamm/7b1c556da389383a06b988a629c1ab43 to your computer and use it in GitHub Desktop.
Save sickHamm/7b1c556da389383a06b988a629c1ab43 to your computer and use it in GitHub Desktop.
company-auto-expand-frontend
(defvar-local company-auto-expand-mode nil)
(defun company-auto-expand-frontend (command)
(when company-auto-expand-mode
(cl-case command
(pre-command
(company--shrink-selection)
(when (and this-command
(company-auto-expand-finish-commmand this-command))
(company-complete-selection)))
(post-command
(company--expand-selection)
(when (overlayp company-preview-overlay)
(company-preview-hide)))
(hide
(setq-local company-auto-expand-mode nil)))))
(defun company--expand-selection ()
(let (company-auto-expand-mode
(company--candidate (nth company-selection
company-candidates)))
(company-call-frontends 'pre-command)
(company--insert-candidate company--candidate)
(let ((company-prefix (substring-no-properties
company--candidate)))
(company-call-frontends 'post-command))))
(defun company--shrink-selection ()
(delete-region company-point (point)))
;; ----------------------
(defun company-auto-expand-finish-commmand (command)
(or (eq command 'self-insert-command)
(not (lookup-command-in-keymap-p command company-active-map))))
;; TODO: more efficient
(defun lookup-command-in-keymap-p (command keymap)
(when (equal command
(catch 'lookup-p
(do-lookup-command-in-keymap-p command keymap)))
t))
(defun do-lookup-command-in-keymap-p (command keymap)
(when (consp keymap)
(let ((sub-keymap (cdr keymap)))
(if (eq command sub-keymap)
(throw 'lookup-p command)
(when (listp sub-keymap)
(mapc (lambda (ssub-keymap)
(do-lookup-command-in-keymap-p command ssub-keymap))
sub-keymap))))))
;; ---------------------------------------------------------------------------------------------------
;; TODO: shoud more common
(defvar company-auto-expand-navigate 'company-complete-common-or-cycle)
(defun company-auto-expand-trigger-navigate (&optional arg)
(interactive "p")
(if company-auto-expand-mode
(let ((current-prefix-arg arg))
(call-interactively company-auto-expand-navigate))
(setq-local company-auto-expand-mode t)))
;; (define-key company-active-map (kbd "<f8>") 'company-auto-expand-trigger-navigate)
(setq company-frontends
'(company-pseudo-tooltip-unless-just-one-frontend
company-preview-if-just-one-frontend
company-auto-expand-frontend ;shoud after any preview frontend
company-echo-metadata-frontend))
(provide 'company-auto-expand-frontend)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment