Ruby-mode.el - font lock not working?

Well, I can’t seem to get it to work correctly. Font locking seems
to be working in all my other buffers, except for ruby buffers.

The correct mode is set, and auto-indenting, etc is working fine.
Syntax highlighting works, but only if I run
font-lock-fontify-buffer. It seems as though the logic to color the
code works, but for some reason it doesn’t happen automatically.
(and, if I modify the code after running font-lock-fontify-buffer,
the newly written code does not get colored correctly)

I’m using this is with GNU Emacs, btw…and a pretty new
version. (from CVS)

···


Josh Huber

Josh,

Josh Huber huber+rt@alum.wpi.edu writes:

Well, I can’t seem to get it to work correctly. Font locking seems
to be working in all my other buffers, except for ruby buffers.

The correct mode is set, and auto-indenting, etc is working fine.
Syntax highlighting works, but only if I run
font-lock-fontify-buffer. It seems as though the logic to color the
code works, but for some reason it doesn’t happen automatically.
(and, if I modify the code after running font-lock-fontify-buffer,
the newly written code does not get colored correctly)

I’m using this is with GNU Emacs, btw…and a pretty new
version. (from CVS)

That happens to me too, and not just in Ruby mode. I have to add a call to
font-lock-mode to the mode hook. Here’s my ruby-mode-hook:

(setq ruby-mode-hook
'(lambda ()
(setq ruby-indent-level 4)
; (setq indent-tabs-mode nil) ; Per advice on c.l.r
(font-lock-mode 1)
))

Jim

···


Jim Menard, jimm@io.com, http://www.io.com/~jimm/
“Unix is extremely user-friendly. It also happens to be extremely
selective when picking its friends.” – Daniel E. Macks

Jim Menard jimm@io.com writes:

That happens to me too, and not just in Ruby mode. I have to add a
call to font-lock-mode to the mode hook. Here’s my ruby-mode-hook:

(setq ruby-mode-hook
'(lambda ()
(setq ruby-indent-level 4)
; (setq indent-tabs-mode nil) ; Per advice on c.l.r
(font-lock-mode 1)
))

This doesn’t help. (in fact, I already tried it…)
global-font-lock-mode is enabled, and font-lock mode is enabled in
the ruby buffer. This appears to be some oddity specific to
ruby-mode, unfortunately. It’s pretty confusing :stuck_out_tongue:

···


Josh Huber

Hi!

  • Jim Menard:

(setq ruby-mode-hook
'(lambda ()
(setq ruby-indent-level 4)
; (setq indent-tabs-mode nil) ; Per advice on c.l.r
(font-lock-mode 1)
))

Some remarks and questions:

What about “(add-hook 'ruby-mode-hook 'turn-on-font-lock)”?

‘(font-lock-mode 1)’ suggests that the argument is a numerical one
while it actually is a logical one. It’s only a question of style but
shouldn’t one rather use ‘(font-lock-mode t)’?

Putting ‘(global-font-lock-mode t)’ into $HOME/.emacs may also be a
good idea.

Does ‘(setq font-lock-maximum-decoration t)’ have any effect in
ruby-mode (I don’t have ruby-mode installed here so I cannot check)?

Josef ‘Jupp’ SCHUGT

···


E-Mail: .— …- .–. .–. .–.-. --. – -…- .-.-.- -… .
http://oss.erdfunkstelle.de/ruby/ - German comp.lang.ruby FAQ
http://rubyforge.org/users/jupp/ - Ruby projects at Rubyforge
Fatal error: ICMP type 3, code 4 while accessing msft.biz .-.-.

Josh Huber wrote:

Jim Menard jimm@io.com writes:

That happens to me too, and not just in Ruby mode. I have to add a
call to font-lock-mode to the mode hook. Here’s my ruby-mode-hook:

(setq ruby-mode-hook
'(lambda ()
(setq ruby-indent-level 4)
; (setq indent-tabs-mode nil) ; Per advice on c.l.r
(font-lock-mode 1)
))

This doesn’t help. (in fact, I already tried it…)
global-font-lock-mode is enabled, and font-lock mode is enabled in
the ruby buffer. This appears to be some oddity specific to
ruby-mode, unfortunately. It’s pretty confusing :stuck_out_tongue:

(Working with the 1.8.1 version of ruby-mode.el) if you apply the
following patch to the ruby-mode.el file, it may work for you (it worked
for me.)

*** /home/asfand/src/packages/ruby/ruby-1.8.1/misc/ruby-mode.el Fri Dec
19 17:29:09 2003
— /home/asfand/.emacs-lisp/ruby-mode.el Sun Mar 14 07:36:51 2004

···

*** 162,166 ****
:options '(t nil space) :group 'ruby)

! (eval-when-compile (require 'cl))
(defun ruby-imenu-create-index-in-block (prefix beg end)
(let ((index-alist '()) (case-fold-search nil)
— 162,169 ----
:options '(t nil space) :group 'ruby)

! (eval-when-compile
! (require 'cl)
! (require 'font-lock)
! )
(defun ruby-imenu-create-index-in-block (prefix beg end)
(let ((index-alist '()) (case-fold-search nil)

“Josef ‘Jupp’ SCHUGT” jupp@gmx.de writes:

Some remarks and questions:

What about “(add-hook 'ruby-mode-hook 'turn-on-font-lock)”?

turn-on-font-lock is an XEmacs thing, not applicable to GNU
Emacs… (i went looking for a similar defun, but it seems like the
equivilant is (font-lock-mode 1))

‘(font-lock-mode 1)’ suggests that the argument is a numerical one
while it actually is a logical one. It’s only a question of style
but shouldn’t one rather use ‘(font-lock-mode t)’?

Perhaps it is with XEmacs? I’ve been trying out GNU Emacs, and this
appears to be a difference in the API:

···

(font-lock-mode &optional ARG)

Toggle Font Lock mode.
With arg, turn Font Lock mode off if and only if arg is a non-positive
number; if arg is nil, toggle Font Lock mode; anything else turns Font
Lock on.

So, it seems it should be a numeric argument.

Putting ‘(global-font-lock-mode t)’ into $HOME/.emacs may also be a
good idea.

I’ve got (global-font-lock-mode 1) in my emacs startup file.
According to the help string for g-f-l-m, it takes a numeric argument
as well…

Does ‘(setq font-lock-maximum-decoration t)’ have any effect in
ruby-mode (I don’t have ruby-mode installed here so I cannot check)?

This variable also doesn’t seem to be used in gnu/emacs…


Josh Huber

Asfand Yar Qazi <im_not_giving_it_here@i_hate_spam.com> writes:

(Working with the 1.8.1 version of ruby-mode.el) if you apply the
following patch to the ruby-mode.el file, it may work for you (it
worked for me.)

Interesting…looks like this is the problem. Do you know why this
happens?

*** /home/asfand/src/packages/ruby/ruby-1.8.1/misc/ruby-mode.el Fri
Dec 19 17:29:09 2003
— /home/asfand/.emacs-lisp/ruby-mode.el Sun Mar 14 07:36:51 2004


*** 162,166 ****
:options '(t nil space) :group 'ruby)

! (eval-when-compile (require 'cl))
(defun ruby-imenu-create-index-in-block (prefix beg end)
(let ((index-alist '()) (case-fold-search nil)
— 162,169 ----
:options '(t nil space) :group 'ruby)

! (eval-when-compile
! (require 'cl)
! (require 'font-lock)
! )
(defun ruby-imenu-create-index-in-block (prefix beg end)
(let ((index-alist '()) (case-fold-search nil)

So, it seems as though just adding

(require 'font-lock) prior to my (require 'ruby-mode) also fixes the
problem. Seems pretty strange, though.

Thanks for the hint!

···


Josh Huber

Hi!

  • Josh Huber:

Some remarks and questions:

What about “(add-hook 'ruby-mode-hook 'turn-on-font-lock)”?

turn-on-font-lock is an XEmacs thing, not applicable to GNU
Emacs…

$ emacs --version|head -1
GNU Emacs 21.2.1

Start, enter

M-x describe-mode font-lock-mode

which among other information displays:

You can enable Font Lock mode in any major mode automatically by
turning on in the major mode’s hook. For example, put in your
~/.emacs:

(add-hook 'c-mode-hook 'turn-on-font-lock)

Switch to scratch

M-x lisp-mode

No highlighting here. Enter

(add-hook 'lisp-mode-hook 'turn-on-font-lock)

enter

M-x eval-current-buffer

again enter

M-x lisp-mode

Voila`: Syntax highlighting.

Does ‘(setq font-lock-maximum-decoration t)’ have any effect in
ruby-mode (I don’t have ruby-mode installed here so I cannot check)?

This variable also doesn’t seem to be used in gnu/emacs…

It definitely works in GNU Emacs. Checked with tex-mode. nil
arguments to textbf (i.e. bold face) display in ordinary face, if set
to t they display in bold face.

TeX files are especially good for testing syntax highlighting:

open file, switch to lisp-mode, mark all lines, M-x comment-region
results in a file that still is okay for TeX syntax highlighting
while at the same time is a lisp file.

You know that the setting to take effect you need to do a
font-lock-mode cycle (i.e. switch it of and on again).

Did I point out that my favorite editor is vim and that I very
rarely use GNU Emacs?

Josef ‘Jupp’ SCHUGT

···


E-Mail: .— …- .–. .–. .–.-. --. – -…- .-.-.- -… .
http://oss.erdfunkstelle.de/ruby/ - German comp.lang.ruby FAQ
http://rubyforge.org/users/jupp/ - Ruby projects at Rubyforge
Fatal error: ICMP type 3, code 4 while accessing msft.biz .-.-.

My workaround is to start xemacs with a scheme file and turn on font lock.
Then when I open ruby files they font lock correctly.

It seems as though I was pretty confused, since in XEmacs C-h a
searches all symbols, while in GNU Emacs it only searches interactive
commands.

No wonder I couldn’t find these functions/variables :slight_smile:

btw, this is with Emacs 21.3.50.1.

“Josef ‘Jupp’ SCHUGT” jupp@gmx.de writes:

$ emacs --version|head -1
GNU Emacs 21.2.1

Start, enter

M-x describe-mode font-lock-mode

which among other information displays:

You can enable Font Lock mode in any major mode automatically by
turning on in the major mode’s hook. For example, put in your
~/.emacs:

(add-hook 'c-mode-hook 'turn-on-font-lock)

Switch to scratch

M-x lisp-mode

No highlighting here. Enter

(add-hook 'lisp-mode-hook 'turn-on-font-lock)

enter

M-x eval-current-buffer

again enter

M-x lisp-mode

Voila`: Syntax highlighting.

Unfortunately, my problem is that in ruby-mode, even when syntax
highlighting is enabled, it doesn’t work until I toggle font-lock
off, then back on again.

It seems as though if I have:

(global-font-lock-mode 1)
(add-hook 'ruby-mode-hook 'turn-on-font-lock)
(setq font-lock-maximum-decoration t)

before I load ruby-mode, then things work as expected, otherwise I
need to toggle font-lock off, then on to make it work.

[… font-lock-maximum-decoration …]
It definitely works in GNU Emacs. Checked with tex-mode. nil
arguments to textbf (i.e. bold face) display in ordinary face, if
set to t they display in bold face.

Yes, I see that now. After reordering as I mentioned above, it seems
to have done the trick.

You know that the setting to take effect you need to do a
font-lock-mode cycle (i.e. switch it of and on again).

perhaps this has something to do with it?

Did I point out that my favorite editor is vim and that I very
rarely use GNU Emacs?

I use vim quite a bit as well, but not for writing code. I like
being able to write elisp to help coding tasks. (of course, I haven’t
had to do this for Ruby, but when dealing with C it’s handy to
automate various boring tasks)

···


Josh Huber