autocomplete-mode not working in web-mode #116
It is very easy to use. Just install autocomplete-mode somewhere and add the following lines to your
That's it – now it works (well, it should) :)
As an elisp novice, I hesitate to suggest ideas about whose implementation I know little.
I think it would be possible to write a web-mode-specific auto-complete source that uses
Just an idea. I don't know how much work would actually be involved there.
I hacked up a quick proof-of-concept this morning:
(defun ac-web-mode-candidates () "Pick the right set of candidates based on position of point context." (let ((cur-web-mode-lang (plist-get (web-mode-point-context (point)) :language))) (cond ((string= cur-web-mode-lang "php") (ac-mode-dictionary 'php-mode)) ((string= cur-web-mode-lang "css") css-property-ids) ((string= cur-web-mode-lang "html") '("div" "script" "testing"))) ) ) (ac-define-source web-mode '((candidates . ac-web-mode-candidates)))
If you load auto-complete and css-mode, turn on auto-complete in web-mode, then run
Making a useful ac-source would take some work - web-mode supports a ton of different server-side systems, and people will want completion sources for different ones.
Just stacking all the possible candidates into one set of candidates might not be good enough - some completion candidates need different actions, and actions are defined in
That makes me think dynamically choosing ac-sources is the right approach, not what I did here.
Anyway, this should at least give some idea what I'm thinking of. I may experiment with the ac-sources idea later.
Okay, I have a prototype of picking auto-complete sources dynamically.
You can see the diff here in my .emacs.d.
The relevant file is web-mode-init.el (the file where I set up my preferences for web-mode). The other changes were to tweak some auto-complete sources to be workable with web-mode.
This change adds three pieces of UI to web-mode:
If those can be solved, I think this should work for adding auto-complete support to web-mode.
Sure. Sorry for any confusion I caused.
Here's a summary of what I'm suggesting. You can refer to https://github.com/NateEag/.emacs.d/compare/web-mode-dynamic-ac-sources?expand=1&w=1#diff-d3ca732e3a4685fe2fe3c612588a2428R1 to see the prototype code I'm describing.
auto-complete mode uses "sources" as backends for computing possible completions. The buffer-local variable "ac-sources" is the interface for choosing which ac-sources are active in a buffer - you just set it to the list of sources you want.
My idea is to add a completion function that sets ac-sources based on the current block's language, then triggers auto-complete. That way, you get the context-aware auto-complete that thkoch2001 asked for.
My function web-mode-trigger-ac does that. It relies on an alist I added (web-mode-ac-sources-alist) that maps language name to a list of ac-sources to activate. That gives users a simple way to choose their auto-complete sources for each block type.
After I had context-aware auto-complete working, I tried to set up emmet-mode's auto-completions in web-mode. Emmet-mode (https://github.com/smihica/emmet-mode) assumes that you are either in an HTML file or a CSS file. To get its completions to work in both block types, I added a pre-auto-completion hook, so that I could tell emmet to be in 'html' or 'css' mode before running auto-complete. I named the hook web-mode-before-auto-complete-hooks.
Does that help?
On Feb 26, 2014, at 10:15 AM, fxbois wrote:
A revamped, simpler POC for context-aware auto-complete in web-mode can be seen in my .emacs.d repo:
is the setup code, and
is the code to register actual ac-sources by programming language.
I don't know whole lot of lisp stuff, but the below lines does auto-completion of both css property name and value:
;; important for css property name auto-complete
(defun my-css-mode-hook ()
Could these be incorporated with NateEag's code?
@fxbois Oops, oversight on my part - I updated most of my config to use
@eN-Joy You should be able to use any ac-source with my code. If you look at https://github.com/NateEag/.emacs.d/blob/86c923c3bae1622633dad1d68143a21845259d00/site-lisp/mode-configs/web-mode-init.el#L25-L55, you would just add
Updated code in my repo, more clearly divided into "proposed addition" and "my config".
Proposed code to add to web-mode:
This look pull-request-worthy?
If so, I can add this to web-mode, add some docs, and issue one.