2015-04-03 04:28:13 +00:00
|
|
|
|
;;; funcs.el --- Auto-completion functions File
|
|
|
|
|
;;
|
2016-01-12 02:40:54 +00:00
|
|
|
|
;; Copyright (c) 2012-2016 Sylvain Benner & Contributors
|
2015-04-03 04:28:13 +00:00
|
|
|
|
;;
|
|
|
|
|
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
|
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
|
;;
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
;;
|
|
|
|
|
;;; License: GPLv3
|
|
|
|
|
|
2016-04-22 01:35:27 +00:00
|
|
|
|
|
|
|
|
|
|
2015-04-03 04:28:13 +00:00
|
|
|
|
(spacemacs|add-toggle auto-completion
|
2015-07-25 13:14:53 +00:00
|
|
|
|
:status
|
2016-03-06 22:20:03 +00:00
|
|
|
|
(if (eq 'company auto-completion-front-end)
|
|
|
|
|
(bound-and-true-p company-mode)
|
|
|
|
|
(bound-and-true-p auto-complete-mode))
|
2015-07-25 13:14:53 +00:00
|
|
|
|
:on
|
|
|
|
|
(progn
|
|
|
|
|
(if (eq 'company auto-completion-front-end)
|
|
|
|
|
(company-mode)
|
|
|
|
|
(auto-complete-mode))
|
|
|
|
|
(message "Enabled auto-completion (using %S)."
|
|
|
|
|
auto-completion-front-end))
|
|
|
|
|
:off
|
|
|
|
|
(progn
|
|
|
|
|
(if (eq 'company auto-completion-front-end)
|
|
|
|
|
(company-mode -1)
|
|
|
|
|
(auto-complete-mode -1))
|
|
|
|
|
(message "Disabled auto-completion."))
|
2015-08-27 06:58:05 +00:00
|
|
|
|
:documentation "Enable auto-completion."
|
2015-07-25 13:14:53 +00:00
|
|
|
|
:evil-leader "ta")
|
2015-04-03 04:28:13 +00:00
|
|
|
|
|
2016-04-22 01:35:27 +00:00
|
|
|
|
|
2015-05-04 04:13:58 +00:00
|
|
|
|
;; auto-completion key bindings functions
|
2015-04-03 04:28:13 +00:00
|
|
|
|
|
2015-05-06 02:32:28 +00:00
|
|
|
|
(defun spacemacs//auto-completion-set-RET-key-behavior (package)
|
|
|
|
|
"Bind RET key appropriately for the given PACKAGE and value of
|
|
|
|
|
`auto-completion-return-key-behavior'."
|
2015-05-04 04:13:58 +00:00
|
|
|
|
(cond
|
|
|
|
|
((eq 'company package)
|
|
|
|
|
(let ((map company-active-map))
|
|
|
|
|
(cond
|
2015-05-06 02:32:28 +00:00
|
|
|
|
((eq 'complete auto-completion-return-key-behavior)
|
2015-05-04 04:13:58 +00:00
|
|
|
|
(define-key map [return] 'company-complete-selection)
|
|
|
|
|
(define-key map (kbd "RET") 'company-complete-selection))
|
|
|
|
|
(t
|
|
|
|
|
(define-key map [return] 'nil)
|
|
|
|
|
(define-key map (kbd "RET") 'nil)))))
|
|
|
|
|
(t (message "Not yet implemented for package %S" package))))
|
|
|
|
|
|
2015-05-06 02:32:28 +00:00
|
|
|
|
(defun spacemacs//auto-completion-set-TAB-key-behavior (package)
|
|
|
|
|
"Bind TAB key appropriately for the given PACKAGE and value of
|
|
|
|
|
`auto-completion-tab-key-behavior'."
|
2015-05-04 04:13:58 +00:00
|
|
|
|
(cond
|
|
|
|
|
((eq 'company package)
|
|
|
|
|
(let ((map company-active-map))
|
|
|
|
|
(cond
|
2015-05-06 02:32:28 +00:00
|
|
|
|
((eq 'complete auto-completion-tab-key-behavior)
|
2015-05-04 04:13:58 +00:00
|
|
|
|
(define-key map (kbd "TAB") 'company-complete-selection)
|
|
|
|
|
(define-key map (kbd "<tab>") 'company-complete-selection))
|
2015-05-06 02:32:28 +00:00
|
|
|
|
((eq 'cycle auto-completion-tab-key-behavior)
|
2015-05-04 04:13:58 +00:00
|
|
|
|
(define-key map (kbd "TAB") 'company-complete-common-or-cycle)
|
|
|
|
|
(define-key map (kbd "<tab>") 'company-complete-common-or-cycle)
|
|
|
|
|
(define-key map (kbd "<S-tab>")
|
|
|
|
|
'spacemacs//company-complete-common-or-cycle-backward)
|
|
|
|
|
(define-key map (kbd "<backtab>")
|
|
|
|
|
'spacemacs//company-complete-common-or-cycle-backward))
|
|
|
|
|
(t
|
|
|
|
|
(define-key map (kbd "TAB") nil)
|
|
|
|
|
(define-key map (kbd "<tab>") nil)))))
|
|
|
|
|
(t (message "Not yet implemented for package %S" package))))
|
|
|
|
|
|
2015-05-06 02:32:28 +00:00
|
|
|
|
(defun spacemacs//auto-completion-setup-key-sequence (package)
|
|
|
|
|
"Setup the key sequence to complete current selection."
|
|
|
|
|
(when auto-completion-complete-with-key-sequence
|
|
|
|
|
(let ((first-key (elt auto-completion-complete-with-key-sequence 0)))
|
|
|
|
|
(cond ((eq 'company package)
|
|
|
|
|
(define-key company-active-map (kbd (char-to-string first-key))
|
|
|
|
|
'spacemacs//auto-completion-key-sequence-start))
|
|
|
|
|
(t (message "Not yet implemented for package %S" package))))))
|
2015-05-04 04:13:58 +00:00
|
|
|
|
|
2016-04-22 01:35:27 +00:00
|
|
|
|
|
2015-05-04 04:13:58 +00:00
|
|
|
|
;; key sequence to complete selection
|
|
|
|
|
|
|
|
|
|
(defvar spacemacs--auto-completion-time nil)
|
|
|
|
|
(defvar spacemacs--auto-completion-complete-last-candidate nil)
|
2015-05-06 02:32:28 +00:00
|
|
|
|
(defvar spacemacs--auto-completion-shadowed-insert-binding nil)
|
|
|
|
|
(defvar spacemacs--auto-completion-shadowed-emacs-binding nil)
|
2015-05-04 04:13:58 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//auto-completion-key-sequence-start ()
|
|
|
|
|
"Initiate auto-completion sequence."
|
|
|
|
|
(interactive)
|
|
|
|
|
(self-insert-command 1)
|
|
|
|
|
(setq spacemacs--auto-completion-complete-last-candidate
|
|
|
|
|
(cond
|
|
|
|
|
((bound-and-true-p company-mode)
|
|
|
|
|
(nth company-selection company-candidates))))
|
2015-05-06 02:32:28 +00:00
|
|
|
|
;; enable second key of the sequence
|
|
|
|
|
(let ((second-key (kbd (char-to-string
|
|
|
|
|
(elt auto-completion-complete-with-key-sequence 1)))))
|
|
|
|
|
(setq spacemacs--auto-completion-shadowed-insert-binding
|
|
|
|
|
(lookup-key evil-insert-state-map second-key))
|
|
|
|
|
(setq spacemacs--auto-completion-shadowed-emacs-binding
|
|
|
|
|
(lookup-key evil-emacs-state-map second-key))
|
|
|
|
|
(define-key
|
|
|
|
|
evil-insert-state-map
|
|
|
|
|
second-key
|
|
|
|
|
'spacemacs//auto-completion-key-sequence-end)
|
|
|
|
|
(define-key
|
|
|
|
|
evil-emacs-state-map
|
|
|
|
|
second-key
|
|
|
|
|
'spacemacs//auto-completion-key-sequence-end))
|
|
|
|
|
;; set a timer to restore the old bindings
|
2015-07-24 12:16:20 +00:00
|
|
|
|
(run-at-time auto-completion-complete-with-key-sequence-delay
|
|
|
|
|
nil
|
|
|
|
|
'spacemacs//auto-completion-key-sequence-restore)
|
2015-05-04 04:13:58 +00:00
|
|
|
|
(when spacemacs--auto-completion-complete-last-candidate
|
|
|
|
|
(setq spacemacs--auto-completion-time (current-time))))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//auto-completion-key-sequence-end ()
|
|
|
|
|
"Check if the auto-completion key sequence has been entered."
|
|
|
|
|
(interactive)
|
|
|
|
|
(if (or (null spacemacs--auto-completion-time)
|
2015-07-24 12:16:20 +00:00
|
|
|
|
(< auto-completion-complete-with-key-sequence-delay
|
|
|
|
|
(float-time (time-since spacemacs--auto-completion-time))))
|
2015-05-04 04:13:58 +00:00
|
|
|
|
(self-insert-command 1)
|
|
|
|
|
(cond
|
|
|
|
|
((bound-and-true-p company-mode)
|
|
|
|
|
(unless company-candidates
|
2015-05-06 02:32:28 +00:00
|
|
|
|
;; if the auto-completion menu is still active then we don't need to
|
|
|
|
|
;; delete the last inserted first key of the sequence
|
|
|
|
|
(delete-char -1))
|
2015-05-04 04:13:58 +00:00
|
|
|
|
(let ((company-idle-delay))
|
|
|
|
|
(company-auto-begin)
|
|
|
|
|
(company-finish spacemacs--auto-completion-complete-last-candidate)))))
|
2015-05-06 02:32:28 +00:00
|
|
|
|
(spacemacs//auto-completion-key-sequence-restore)
|
2015-05-04 04:13:58 +00:00
|
|
|
|
(setq spacemacs--auto-completion-time nil))
|
2015-05-06 02:32:28 +00:00
|
|
|
|
|
|
|
|
|
(defun spacemacs//auto-completion-key-sequence-restore ()
|
|
|
|
|
"Restore the shadowed key bindings used to auto-complete."
|
|
|
|
|
(let ((second-key (kbd (char-to-string
|
|
|
|
|
(elt auto-completion-complete-with-key-sequence 1)))))
|
|
|
|
|
(define-key
|
|
|
|
|
evil-insert-state-map
|
|
|
|
|
second-key
|
|
|
|
|
spacemacs--auto-completion-shadowed-insert-binding)
|
|
|
|
|
(define-key
|
|
|
|
|
evil-emacs-state-map
|
|
|
|
|
second-key
|
|
|
|
|
spacemacs--auto-completion-shadowed-emacs-binding)))
|
2016-04-22 01:35:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Editing style
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//company-active-navigation (style)
|
|
|
|
|
"Set navigation for the given editing STYLE."
|
|
|
|
|
(cond
|
|
|
|
|
((or (eq 'vim style)
|
|
|
|
|
(and (eq 'hybrid style)
|
|
|
|
|
hybrid-mode-enable-hjkl-bindings))
|
|
|
|
|
(let ((map company-active-map))
|
|
|
|
|
(define-key map (kbd "C-j") 'company-select-next)
|
|
|
|
|
(define-key map (kbd "C-k") 'company-select-previous)
|
|
|
|
|
(define-key map (kbd "C-l") 'company-complete-selection)))
|
|
|
|
|
(t
|
|
|
|
|
(let ((map company-active-map))
|
|
|
|
|
(define-key map (kbd "C-n") 'company-select-next)
|
|
|
|
|
(define-key map (kbd "C-p") 'company-select-previous)
|
|
|
|
|
(define-key map (kbd "C-f") 'company-complete-selection)))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Transformers
|
|
|
|
|
|
|
|
|
|
(defun spacemacs//company-transformer-cancel (candidates)
|
|
|
|
|
"Cancel completion if prefix is in the list
|
|
|
|
|
`company-mode-completion-cancel-keywords'"
|
|
|
|
|
(unless (member company-prefix company-mode-completion-cancel-keywords)
|
|
|
|
|
candidates))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defvar-local company-fci-mode-on-p nil)
|
|
|
|
|
|
|
|
|
|
(defun company-turn-off-fci (&rest ignore)
|
|
|
|
|
(when (boundp 'fci-mode)
|
|
|
|
|
(setq company-fci-mode-on-p fci-mode)
|
|
|
|
|
(when fci-mode (fci-mode -1))))
|
|
|
|
|
|
|
|
|
|
(defun company-maybe-turn-on-fci (&rest ignore)
|
|
|
|
|
(when company-fci-mode-on-p (fci-mode 1)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; helm-yas
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/helm-yas ()
|
|
|
|
|
"Properly lazy load helm-c-yasnipper."
|
|
|
|
|
(interactive)
|
|
|
|
|
(spacemacs/load-yasnippet)
|
|
|
|
|
(require 'helm-c-yasnippet)
|
|
|
|
|
(call-interactively 'helm-yas-complete))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Yasnippet
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/load-yasnippet ()
|
|
|
|
|
(unless yas-global-mode (yas-global-mode 1))
|
|
|
|
|
(yas-minor-mode 1))
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/force-yasnippet-off ()
|
|
|
|
|
(yas-minor-mode -1)
|
|
|
|
|
(setq yas-dont-activate t))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; Auto-Yasnippet
|
|
|
|
|
|
|
|
|
|
(defun spacemacs/auto-yasnippet-expand ()
|
|
|
|
|
"Call `yas-expand' and switch to `insert state'"
|
|
|
|
|
(interactive)
|
|
|
|
|
(call-interactively 'aya-expand)
|
|
|
|
|
(unless holy-mode (evil-insert-state)))
|