I had the same problem as you so I decided to write web-mode.
It doesn’t rely on any other mode. It does syntax highlight et indenting according to the code block type. I’ve written a little HOWTO page on
web-mode.elis will never be a full featured PHP or JS mode. (We don’t encourage multi-lines blocs of code in templates !).
- code navigation :
C-c C-nbetween opening and closing HTML tag or between PHP controls > – if … else … endif, for … endfor
- HTML tag autoclosing (after
- syntax highlighting (according to the type of bloc)
- snippet insertion
C-c C-i(auto indented, aware of text selection)
- auto-inserts (ex.
I’ve been dissatisified with the available modes, but I have found happiness recently using plain old
html-mode augmented with
tidy.el. As the other answers have mentioned nxhtml mode is useful if you are writing xhtml.
mmm-mode, which will allow you to run multiple major modes at once. I’ve never used it, so I don’t know who it handles conflicting key bindings, etc… so you’ll have to feel your way around there.
Give multi-web mode a try, it’s a minor mode designed with web editing in mind, it selects the appropriate major when the point moves around.
I too use nXhtml mode for mixed mode editing having failed to get Multiple Modes working correctly. It has been a bit of a rocky ride but recent builds have proved to be useful. I’m running with Emacs 23 FWIW.
;; https://polymode.github.io/installation/#activation-of-polymodes (require 'polymode) (define-hostmode poly-html-hostmode :mode 'html-mode) (define-innermode poly-html-js-metadata-innermode :mode 'js-mode :head-matcher "^[ \t]*<script.*>" :tail-matcher "^[ \t]*</script.*>" :head-mode 'host :tail-mode 'host :body-indent-offset '2 ) (define-innermode poly-html-css-metadata-innermode :mode 'css-mode :head-matcher "^[ \t]*<style.*>" :tail-matcher "^[ \t]*</style.*>" :head-mode 'host :tail-mode 'host :body-indent-offset '2 ) ;; html host with js and css innermodes (define-polymode poly-html-mode :hostmode 'poly-html-hostmode :innermodes '(poly-html-js-metadata-innermode poly-html-css-metadata-innermode) ) ;; set mode for html files (add-to-list 'auto-mode-alist '("\\.html\\'" . poly-html-mode))
I put this type of stuff in a local elisp file and load it my .emacs for testing and factoring.