1

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 that 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.

Renaud
  • 8,783
  • 4
  • 32
  • 40
  • can you clarify the problem you're trying to solve? Is the problem with the snippet? It seems not, because you said `The snippet works as intended...` . In that case, why do you even mention the snippet in this question? And what does it mean to say `the completion don't though the auto-completion works...`. Does completion work, or not? And you also sprinkled in your question this statement: `I failed to enable the auto-complete mode.` Is *that* the real problem? Maybe you could use a few more sentences. Clarify what you are asking. – Cheeso Sep 16 '11 at 00:58
  • Also, if you want to fill a file in with a given template, automatically based on its name, you can try http://www.emacswiki.org/emacs/DefaultContent – Cheeso Sep 16 '11 at 01:09
  • I mention yasnippet since I want to auto complete yasnippet keys. My meaning is that the auto completion works well when I enable manualy the auto-complete mode. The issue is that I failed to auto enable auto-complete with a hook on nxml-mode. I confirm that I don't need a file template (that's off topic but android project does init the AndroidManifest file, I bref templates for activity, permission, etc.) – Renaud Sep 16 '11 at 05:51
  • A part of the last sentence has been corrupted by my android phone (and native language completion): I *need* templates for activity, permission. – Renaud Sep 16 '11 at 07:12

1 Answers1

0

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)
Renaud
  • 8,783
  • 4
  • 32
  • 40