nXhtml: tame your rhtml files

nXhtml.png

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! :D

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值