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:
(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-tabs-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
Comments (15)
Hi,the nxhtml helps a lot.
But seems there is some problem,I found a line "File mode specification error: (void-function rails-minor-mode)" in the *Message* buffer,why?
thanks for the post.
Hi! Maybe you didn't install the emacs-rails extension. Then just remove or comment the 30-th line in the example above:
Thanks Pluskid for your kindly reply!
Yes I finally found the reason,and now installed emacs-rails extension,and ecb/cedet,it seems wonderful to develop rails application with Emacs now!
你是指在 nxhtml-mode 的多模式下?比如,编辑 js 或者 css 的时候?这个我倒是还没有尝试过,不知道它是使用什么方式切换 major-mode 的,也许这影响到 YASnippet 的 minor-mode 的开启了。你如果在 mode-line 上没有看到 YAS 的字样的话应该是 minor-mode 没能自动打开。
如果它的 mode-name 是 nXhtml/nxhtml-mode 的话估计有问题……你试试看
C-h k再按TAB键看看TAB是不是被绑定到yas/expand上了?没有绑定给yasnippet:
要是yasnippet能自动插入到这种复合模式中工作那就完美了。nxhtml-mode自带的自动完成功能不怎么好用,好像是用的是snippet.el,因为我对elisp很不熟,只好到处求助,可google出来的yasnippet相关信息太少了。如果你也很喜欢nxhtml-mode的话,能不能和它的作者沟通一下,把yasnippet作为它的默认tab complete?
@transtone, 汗……你把最关键的信息去掉了,我主要是想知道那里是
TAB还是。如果是后者的话,应该好解决,试试:(add-hook 'nxhtml-mode-hook '(lambda () (local-set-key [tab] 'yas/expand)))恩,还有诸如 Emacs 的 rails 支持等好像都是用的 snippet ,等我有时间的时候也是得考虑一下和这些作者沟通一下,哈哈! ^_^ 多谢支持!
不是我去掉的,是输入到Comment后自动去掉的。 是,<tab> 还不太清楚本博客支持的格式,bbcode吗?
添加了那个绑定,但还是无效。 都把这变成论坛了 :) 下面是C-h m 后的全部信息:
呵呵,好多内容。看样子 nxhtml-mode 也是一个 minor-mode ,好像它占优先的样子。你在设置了
local-set-key的 hook 之后重新打开那个文件,再C-h k看是绑定到yas/expand还是indent-for-tab-command啊?@transtone, 这样的,看那里开了好多的 minor-mode ,看样子好像
键被某个 mode 抢先了,我好像没有在 nxhtml-mode 的代码里找到相关的东西。你试试手工M-x yas/expand,如果能展开的话就是按键的问题,可以尝试这样:如果实在不行的话也许可以考虑换一个按键。