all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Alan Mackenzie <acm@muc.de>
To: help-gnu-emacs@gnu.org
Subject: Re: c-mode and underscore
Date: Wed, 8 Jul 2009 10:52:14 +0000 (UTC)	[thread overview]
Message-ID: <h31tou$bqs$1@colin2.muc.de> (raw)
In-Reply-To: mailman.2043.1247031632.2239.help-gnu-emacs@gnu.org

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).



  parent reply	other threads:[~2009-07-08 10:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-07 22:56 c-mode and underscore geophile
2009-07-07 23:58 ` Miles Bader
2009-07-08  2:16 ` Xah Lee
2009-07-08  5:40   ` Kevin Rodgers
     [not found]   ` <mailman.2043.1247031632.2239.help-gnu-emacs@gnu.org>
2009-07-08 10:44     ` Xah Lee
2009-07-08 10:52     ` Alan Mackenzie [this message]
2009-07-08 18:57       ` geophile

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='h31tou$bqs$1@colin2.muc.de' \
    --to=acm@muc.de \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.