Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the yasnippet as the auto-complete backend?

I want to make the yasnippet as the backend of the auto-complete. However, it doesn't work. what I do after searched the internet is as follows: get the auto-complete-yasnippet.el, add some elisp in the .emacs like this:

(add-to-list 'load-path
     "~/.emacs.d/plugins/yasnippet")
(require 'yasnippet)
(yas-global-mode 1)
(yas-minor-mode nil)
(global-set-key (kbd "M-/") 'yas/expand)

;; Auto-complete settings
;; this is the code for the auto-complete
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d//ac-dict")
(ac-config-default)

;;setup for auto-complete-yasnippet
(require 'auto-complete-yasnippet)
(setq-default ac-sources
      '(
        ;; ac-source-semantic
        ac-source-yasnippet
        ac-source-abbrev
        ac-source-words-in-buffer
        ac-source-words-in-all-buffer
        ;; ac-source-imenu
        ac-source-files-in-current-dir
        ac-source-filename
        )
      )

I look at the content in the ac-sources in *scratch* with C-h v, and it does have the ac-source-yasnippet. someone said that there may be something wrong with the version and upgrade of auto-complete as well as yasnippet. How can it be fixed? My emacs version is 23.3.1 my auto-complete version is 1.3.1 and my yasnippet version is 0.8.0(beta) which is just downloaded from the github. any help?

like image 355
toolchainX Avatar asked Oct 07 '22 00:10

toolchainX


2 Answers

you can easily do it by (require 'auto-complete-yasnippet)

and then you can change your auto-complete like follows:

(defun my-ac-config ()
  (setq-default ac-sources '(ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))
  (add-hook 'emacs-lisp-mode-hook 'ac-emacs-lisp-mode-setup)
  ;; (add-hook 'c-mode-common-hook 'ac-cc-mode-setup)
  (add-hook 'ruby-mode-hook 'ac-ruby-mode-setup)
  (add-hook 'css-mode-hook 'ac-css-mode-setup)
  (add-hook 'auto-complete-mode-hook 'ac-common-setup)
  (add-hook 'octave-mode-hook 'ac-octave-mode-setup)
  (global-auto-complete-mode t))
(defun my-ac-cc-mode-setup ()
  (setq ac-sources (append '(ac-source-clang ac-source-yasnippet) ac-sources)))
(add-hook 'c-mode-common-hook 'my-ac-cc-mode-setup)
;; ac-source-gtags
(my-ac-config)

It works fine on my machine.

like image 152
madper Avatar answered Oct 10 '22 04:10

madper


there maybe something wrong with the auto-complete-config.el when acquire the ac-yasnippet-candidates in the auto-complete-1.3.1 version:

(defun ac-yasnippet-candidates ()
  (with-no-warnings
    (if (fboundp 'yas/get-snippet-tables)
        ;; >0.6.0
        (apply 'append (mapcar 'ac-yasnippet-candidate-1 (yas/get-snippet-tables major-mode)))
      (let ((table
             (if (fboundp 'yas/snippet-table)
                 ;; <0.6.0
                 (yas/snippet-table major-mode)
               ;; 0.6.0
               (yas/current-snippet-table))))
        (if table
            (ac-yasnippet-candidate-1 table))))))

the code snippet above somewhat must be changed to be compatible with the yasnippet-0.8.0version. I download the newest version of auto-complete-1.4.0 from github and it solved the problem judge the version of yasnippet and take measures accordingly. Like this:

(defun ac-yasnippet-candidates ()
  (with-no-warnings
    (cond (;; 0.8 onwards
           (fboundp 'yas-active-keys)
           (all-completions ac-prefix (yas-active-keys)))
          (;; >0.6.0
           (fboundp 'yas/get-snippet-tables)
           (apply 'append (mapcar 'ac-yasnippet-candidate-1
                                  (condition-case nil
                                      (yas/get-snippet-tables major-mode)
                                    (wrong-number-of-arguments
                                     (yas/get-snippet-tables))))))
          (t
           (let ((table
                  (if (fboundp 'yas/snippet-table)
                      ;; <0.6.0
                      (yas/snippet-table major-mode)
                    ;; 0.6.0
                    (yas/current-snippet-table))))
             (if table
                 (ac-yasnippet-candidate-1 table)))))))

I copied the auto-complete-config.el from the auto-complete-1.4.0version, byte compiled it, and replaced the same files(both auto-complete-config.el and auto-complete-config.elc) in auto-complete-1.3.1version. it just worked! I think may the config file of auto-complete should not included in the distro and maybe it should maintained separately to make it easy to compatible with its backends.

I reconfigured the yasnippet and the auto-complete like this:

;; setup for yasnippet
(add-to-list 'load-path
      "~/.emacs.d/plugins/yasnippet")
;; Extension and configuration of yasnippet.
(require 'yasnippet-config)
;; If you use yasnippet from 'auto-complete', add
(yas/set-ac-modes)
(yas/enable-emacs-lisp-paren-hack)
;; before 'auto-complete' settings.
;; Auto-complete settings
;; this is the code for the auto-complete
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d//ac-dict")
(ac-config-default)
like image 45
toolchainX Avatar answered Oct 10 '22 02:10

toolchainX