如何将yasnippet作为自动完成后端?

时间:2012-11-13 12:40:19

标签: emacs autocomplete yasnippet

我想将yasnippet作为auto-complete的后端。但是,它不起作用。搜索互联网后我做的如下: 获取auto-complete-yasnippet.el,在.emacs中添加一些elisp:

(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
        )
      )

我用 C-h v 查看* {*}中的{* 1}}中的内容,它确实有ac-sources。某人said ac-source-yasnippet以及auto-complete的版本和升级可能存在问题。怎么修好? 我的yasnippet版本为23.3.1我的emacs版本为1.3.1,我的auto-complete版本为0.8.0(测试版),刚刚从github下载。有什么帮助吗?

2 个答案:

答案 0 :(得分:2)

auto-complete-config.el版本中获取ac-yasnippet-candidatesauto-complete-1.3.1可能出现问题:

(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))))))

上面的代码段必须更改为与yasnippet-0.8.0版本兼容。我从github下载了auto-complete-1.4.0的最新版本,它解决了问题判断yasnippet的版本并采取相应措施。像这样:

(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)))))))

我从auto-complete-config.el版本中复制了auto-complete-1.4.0,对其进行了字节编译,并在auto-complete-config.el版本中替换了相同的文件(auto-complete-config.elcauto-complete-1.3.1) 。它刚刚奏效!我认为auto-complete的配置文件可能不应该包含在发行版中,也许它应该单独维护以便于与后端兼容。

我重新配置了yasnippetauto-complete,如下所示:

;; 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)

答案 1 :(得分:1)

您可以通过(require 'auto-complete-yasnippet)

轻松完成

然后您可以更改自动完成,如下所示:

(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)

它在我的机器上工作正常。