help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: c-mode and underscore


From: Alan Mackenzie
Subject: Re: c-mode and underscore
Date: Wed, 8 Jul 2009 10:52:14 +0000 (UTC)
User-agent: tin/1.6.2-20030910 ("Pabbay") (UNIX) (FreeBSD/4.11-RELEASE (i386))

Hi, Kevin!

Kevin Rodgers <kevin.d.rodgers@gmail.com> wrote:
> Xah Lee wrote:
>> On Jul 7, 3:56 pm, geophile <jack.orenst...@gmail.com> wrote:
>>> I am trying to get c-mode to treat underscore as a word, so that
>>> forward-word backward-word don't stop on underscores.

>>> My .emacs file includes:

>>>     (modify-syntax-entry ?_ "w" c-mode-syntax-table)

>>> which does not appear to be effective. But if I run this command
>>> manually, it is effective.

>>> I'm pretty sure that the line above is being reached in my .emacs
>>> file, as later commands are effective.

> How could c-mode-syntax-table refer to the current (not global) syntax
> table?

> It is consistent with the hypothesis that the problem is that
> c-mode-syntax-table does not have its correct value when .emacs is
> loaded.  Indeed, this code from progmodes/cc-mode.el reveals why it
> is nil when .emacs is loaded and thus does refer to the current syntax
> table as you said:

> ;;;###autoload
> (defvar c-mode-syntax-table nil
>   "Syntax table used in c-mode buffers.")
> (or c-mode-syntax-table
>     (setq c-mode-syntax-table
>          (funcall (c-lang-const c-make-mode-syntax-table c))))

> The autoload cookie causes the defvar to be copied into loaddefs.el
> and thus dumped into the emacs executable.  Why does cc-mode.el do that
> instead of the obvious

> (defvar c-mode-syntax-table (funcall (c-lang-const 
>   c-make-mode-syntax-table c))
>   "Syntax table used in c-mode buffers.")

Can't really say, but I suppose I can guess.  This construct goes back to
the very beginning of CC Mode.  Version 1.1 of cc-mode.c from 1992-03-13
contains these lines:

    (defvar c++-mode-syntax-table nil
      "Syntax table in use in C++-mode buffers.")

    (if c++-mode-syntax-table
        ()
      (setq c++-mode-syntax-table (copy-syntax-table c-mode-syntax-table))
      (modify-syntax-entry ?/ ". 12" c++-mode-syntax-table)
      (modify-syntax-entry ?\n ">" c++-mode-syntax-table)
      (modify-syntax-entry ?\' "." c++-mode-syntax-table))

The XEmacs (or was it still Lucid Emacs?) of the time may have lacked the
ability to initialise a variable from an arbitrary expression.  The
maintainer at the time, Barry Warsaw, was engaged in a massive exercise
merging his C++ Mode with "Boring Old C Mode" to create the new shiny
CC Mode.  (It's still shining 17 years later, so he must have done a
good job.)  At such times, exactly how you initialise a variable seems
trivial and unimportant, and perhaps Barry was intending to make that
change, but never got round to it.  After all, it worked, and there was
plenty of other stuff to do.

> If you're going to use a hook, you may as well just refer to the current
> syntax table with (syntax-table) instead of by name.

That's kind of a bit awkward from .emacs.

I think the best way is to use c-initialization-hook.  Such as:

    (defun jo-init-function ()
      "Doc string ....."
      (modify-syntax-entry ?_ "w" c-mode-syntax-table))
    (add-hook 'c-initialization-hook 'jo-init-function)

To the OP: make sure this appears in your .emacs BEFORE anything which
loads CC Mode.

-- 
Alan Mackenzie (Nuremberg, Germany).



reply via email to

[Prev in Thread] Current Thread [Next in Thread]