nXhtml: tame your rhtml files

阅读更多

I personally use Emacs as an IDE to develop rails project. Equipped with emacs-rails, it becomes a powerful IDE for rails developing.

However, you may noticed, it doesn’t have full support for rhtml files. You can use html-mode to edit rhtml files, it is already good enough. But is is of course not as good as other wonderful modes(e.g. ruby-mode) Emacs supplied.

Other choices is mmm-mode, two-mode-mode or some similar things. But it is a little complicated to config while the results is not very impressing.

There’s also a custom-made mode for rhtml files in Rinari: rhtml-mode. It might be good. Bug I guess there’s something wrong with the code. Emacs may become slow when I turn on this mode: a latency can be felt even to move the cursor up and down.

I still use the plain old html-mode to edit those rhtml files, until I finally find the godsends nXhtml package. It has a nxhtml-mode which can recognize plenty of embedded fragment including javascript, php and, of course, ruby, etc.

It is really an amazing package! And the installation is very simple. Just download the latest nXhtml zip package. Unzip it and load the autostart.el file in your Emacs.

However, as an Emacs geek, I always want to configure it to my own style. So here comes my configuration for nXhtml to edit rhtml files:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(load "~/emacs/packages/nxml/autostart.el")
(add-to-list 'auto-mode-alist
             '("\\.rhtml$" . kid-rhtml-mode))
;; only special background in submode
(setq mumamo-chunk-coloring 'submode-colored)
(setq nxhtml-skip-welcome t)
 
;; do not turn on rng-validate-mode automatically, I don't like
;; the anoying red underlines
(setq rng-nxml-auto-validate-flag nil)
 
;; force to load another css-mode, the css-mode in nxml package
;; seems failed to load under my Emacs 23
(let ((load-path (cons "~/emacs/extension/"
                       load-path)))
  (require 'css-mode))
 
(defun kid-rhtml-mode ()
  (nxhtml-mode)
  ;; I don't use cua-mode, but nxhtml always complains. So, OK, let's
  ;; define this dummy variable
  (make-local-variable 'cua-inhibit-cua-keys)
  (setq mumamo-current-chunk-family '("eRuby nXhtml Family" nxhtml-mode
                                      (mumamo-chunk-eruby
                                       mumamo-chunk-inlined-style
                                       mumamo-chunk-inlined-script
                                       mumamo-chunk-style=
                                       mumamo-chunk-onjs=)))
  (mumamo-mode)
  (rails-minor-mode t)
  (auto-fill-mode -1)
  (setq tab-width 2)
  (setq indent-tab-mode nil))

You can see in the screenshot the nxhtml-mode recognize both ERB fragment and javascript fragment. It can also handle css fragment if you have proper css-mode installed(nxml comes with a css-mode, but it refuses to load with my Emacs 23). Wonderful!

你可能感兴趣的:(Emacs,Rails,CSS,Ruby,JavaScript)