Make auto-complete and yasnippet modes work together to edit a specific file in GNU/Emacs
I use Emacs 24 from scratch with the latest yasnippet and auto-complete installed and nominally working. Now, as a emacs user and an android developer, I'd like to use my favorite editor and automate some tasks fr android development. I know almost nothing about elisp.
My first task is to use custom snippets to add the uses-sdk tag in AndroidManifest.xml. That's ok with yasnippet but I'd like to use auto-complete to interactively propose and auto-complete android specific tags. The problem is that the major mode for AndroidManifest.xml is nxml and I don't want to propose android specifics to all nxml-mode related buffers. As a consequence, I use a condition on the buffer name in the snippet definition. Now, I'd like to add a custom hook to nxml-mode-hook but I failed to enable the auto-complete mode.
My snippet:
#contributor : Me, Myself and I
#name : <uses-sdk ... />
#condition : (string= (buffer-name) "AndroidManifest.xml")
# --
<uses-sdk android:minSdkVersion="$0" />
The .emacs part tha开发者_JAVA技巧t miserably failed:
;; yasnippet
(add-to-list 'load-path "~/.emacs.d/yasnippet")
(require 'yasnippet)
(setq yas/trigger-key (kbd "C-c <kp-multiply>"))
(yas/initialize)
;; Develop in ~/emacs.d/mysnippets, but also
;; try out snippets in ~/Downloads/interesting-snippets
(setq yas/root-directory '("~/.emacs.d/snippets"
"~/.emacs.d/external-snippets"))
;; Map `yas/load-directory' to every element
(mapc 'yas/load-directory yas/root-directory)
;; auto-complete
(add-to-list 'load-path "~/.emacs.d/auto-complete")
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/auto-complete/ac-dict")
(setq-default ac-sources '(ac-source-yasnippet 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)
(global-auto-complete-mode t)
;; android specific settings
;; AndroidManifest.xml
(defun ac-android-manifest-nxml-setup()
""
(when (string= (buffer-name) "AndroidManifest.xml")
(setq ac-sources '(ac-source-yasnippet ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))))
(add-hook 'nxml-mode-hook 'ac-android-manifest-nxml-setup)
The snippet works as intended but the completion don't though the auto-completion works if I enable auto-complete using M-x auto-complete-mode
.
Any help will be greatly appreciated.
Works well with
;; auto-complete
(add-to-list 'load-path "~/.emacs.d/auto-complete")
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/auto-complete/ac-dict")
(ac-config-default)
;; android specific settings
;; AndroidManifest.xml
(defun ac-android-manifest-nxml-setup()
(when (string= (buffer-name) "AndroidManifest.xml")
(setq ac-sources '(ac-source-yasnippet
ac-source-abbrev
ac-source-dictionary
ac-source-words-in-same-mode-buffers))
((lambda () (auto-complete-mode 1)))))
(add-hook 'nxml-mode-hook 'ac-android-manifest-nxml-setup)
精彩评论