Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
better counsel-company
;; author: @fuxialexander on github
;; 2017-12-02
(defvar company-candidates)
(defvar company-point)
(defvar company-common)
(declare-function company-complete "ext:company")
(declare-function company-mode "ext:company")
(declare-function company-complete-common "ext:company")
(defun counsel-company-show-doc-buffer (selected)
"Temporarily show the documentation buffer for the selection."
(let (other-window-scroll-buffer)
(let* ((doc-buffer (or (company-call-backend 'doc-buffer selected)
(user-error "No documentation available")))
(when (consp doc-buffer)
(setq start (cdr doc-buffer)
doc-buffer (car doc-buffer)))
(setq other-window-scroll-buffer (get-buffer doc-buffer))
(let ((win (display-buffer doc-buffer t)))
(set-window-start win (if start start (point-min))))))))
(defun counsel-company-action (str)
"Insert STR, erasing the previous one.
The previous string is between `ivy-completion-beg' and `ivy-completion-end'."
(when (consp str)
(setq str (cdr str)))
(when (stringp str)
(let ((fake-cursors (and (fboundp 'mc/all-fake-cursors)
(pt (point))
(beg ivy-completion-beg)
(end ivy-completion-end))
(when ivy-completion-beg
(setq ivy-completion-beg
(move-marker (make-marker) (point)))
(insert (substring-no-properties str))
(setq ivy-completion-end
(move-marker (make-marker) (point)))
(dolist (cursor fake-cursors)
(goto-char (overlay-start cursor))
(delete-region (+ (point) (- beg pt))
(+ (point) (- end pt)))
(insert (substring-no-properties str))
;; manually move the fake cursor
(move-overlay cursor (point) (1+ (point)))
(move-marker (overlay-get cursor 'point) (point))
(move-marker (overlay-get cursor 'mark) (point))))))
(defun counsel-company ()
"Complete using `company-candidates'."
(company-mode 1)
(unless company-candidates
(when company-point
(when (looking-back company-common (line-beginning-position))
(setq ivy-completion-beg (match-beginning 0))
(setq ivy-completion-end (match-end 0)))
(ivy-read "company cand: " company-candidates
:predicate (lambda (candidate)
(string-prefix-p prefix (car candidate)))
:caller 'counsel-company
:action 'counsel-company-action)))
(add-to-list 'ivy-display-functions-alist '(counsel-company . ivy-display-function-overlay))
("d" counsel-company-show-doc-buffer "Doc")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment