Skip to content

Instantly share code, notes, and snippets.

@sickHamm
Created June 29, 2016 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sickHamm/c5cf1eecceadcf004fa0322a16c67ea2 to your computer and use it in GitHub Desktop.
Save sickHamm/c5cf1eecceadcf004fa0322a16c67ea2 to your computer and use it in GitHub Desktop.
;;; company-auto-expand-mode.el --- auto expand company-mode selection candidate
;; Author: Yu Guang
;;; Commentary:
;;
;; The core code is very simple; just shrink company candidate brefore every command
;; and expand company candidate again after every command;
;; dispaly at here maybe useful for someone.
;; (defun company-auto-expand-frontend (command)
;; (cl-case command
;; (pre-command
;; (delete-region company-point (point))
;; (when (eq this-command 'self-insert-command)
;; (company-complete-selection)))
;; (post-command
;; (company--insert-candidate
;; (nth company-selection company-candidates)))))
;; Have a minor bug when work together with company-pseudo-tooltip-unless-just-one-frontend-with-delay
;; you could use (if (and (not (overlayp company-preview-overlay)) (not company--auto-expand-inline)))
;; replace this line (if (not (overlayp company-preview-overlay)) in company-pseudo-....-with-delay.
;;; code:
(require 'company)
(defgroup company-auto-expand-mode nil
"auto expand company-mode selection candidate"
:group 'company)
(defcustom company-auto-expand-navigate-command
'company-select-next
"which command will active company-auto-expand"
:type 'symbol
:group 'company-auto-expand-mode)
;; ---------------------------------------------------------------------------------------------------
(defvar-local company-auto-expand-active nil)
(defvar-local company--auto-expand-inline nil)
(defun company-auto-expand-frontend (command)
(when company-auto-expand-active
(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))
(hide
(setq-local company-auto-expand-active nil)))))
(defun company--expand-selection ()
(unless company--auto-expand-inline
(let (company-auto-expand-active
(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)))
(when (overlayp company-preview-overlay)
(company-preview-hide))
(setq-local company--auto-expand-inline t)))
(defun company--shrink-selection ()
(when company--auto-expand-inline
(delete-region company-point (point))
(setq-local company--auto-expand-inline nil)))
;; ----------------------
(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
(defun company-auto-expand-navigate-advice (orig-command &optional arg)
(interactive "p")
(if company-auto-expand-active
(let ((current-prefix-arg arg))
(call-interactively orig-command))
(setq-local company-auto-expand-active t)))
;;;###autoload
(define-minor-mode company-auto-expand-mode
"A minor mode for company-mode, turn on will auto expand selection candidate.
Usage: add company-auto-expand-frontend to company-frontends (should place behind
pseudo-tooltip and preview) and turn on this minor mode when use company-mode."
nil nil nil
:global t
(if company-auto-expand-mode
(if company-mode
(advice-add company-auto-expand-navigate-command
:around #'company-auto-expand-navigate-advice)
(message "Only Turn-on company-auto-expand-mode when company-mode ON")
(company-auto-expand-mode -1))
(when (advice-member-p #'company-auto-expand-navigate-advice
company-auto-expand-navigate-command)
(advice-remove company-auto-expand-navigate-command
#'company-auto-expand-navigate-advice))))
(provide 'company-auto-expand-mode)
;;; company-auto-expand-mode.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment