all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* c-mode and underscore
@ 2009-07-07 22:56 geophile
  2009-07-07 23:58 ` Miles Bader
  2009-07-08  2:16 ` Xah Lee
  0 siblings, 2 replies; 7+ messages in thread
From: geophile @ 2009-07-07 22:56 UTC (permalink / raw)
  To: help-gnu-emacs

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.

Any idea what I'm doing wrong?

Jack Orenstein


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: c-mode and underscore
  2009-07-07 22:56 c-mode and underscore geophile
@ 2009-07-07 23:58 ` Miles Bader
  2009-07-08  2:16 ` Xah Lee
  1 sibling, 0 replies; 7+ messages in thread
From: Miles Bader @ 2009-07-07 23:58 UTC (permalink / raw)
  To: help-gnu-emacs

geophile <jack.orenstein@gmail.com> writes:
> I am trying to get c-mode to treat underscore as a word, so that
> forward-word backward-word don't stop on underscores.

Instead of doing that, try to use the "sexp" movement commands, which
move over symbols instead of words.

C-M-f = forward-sexp
C-M-b = backward-sexp
etc.

-Miles

-- 
Learning, n. The kind of ignorance distinguishing the studious.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: c-mode and underscore
  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>
  1 sibling, 2 replies; 7+ messages in thread
From: Xah Lee @ 2009-07-08  2:16 UTC (permalink / raw)
  To: help-gnu-emacs

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.

your code mod the global syntax table. you want to mode the syntax
table for that mode. It works when u call manually because when u are
in that mode, it mods that mod's syntax table.

hook is a good solution.
e.g.

(add-hook 'w3m-mode-hook
 (lambda ()
  (define-key w3m-mode-map (kbd "<up>") 'previous-line) ; was w3m-
previous-anchor. Use Shift+Tab.
  (define-key w3m-mode-map (kbd "<down>") 'next-line) ; was w3m-next-
anchor. Use Tab.
  (define-key w3m-mode-map (kbd "<left>") 'backward-char) ; was w3m-
view-previous-page. Use B.
  (define-key w3m-mode-map (kbd "<right>") 'forward-char) ; was w3m-
view-this-url. Use Enter.
))

you want to find the syntax table name for that mode to modify.

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: c-mode and underscore
  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>
  1 sibling, 0 replies; 7+ messages in thread
From: Kevin Rodgers @ 2009-07-08  5:40 UTC (permalink / raw)
  To: help-gnu-emacs

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.
> 
> your code mod the global syntax table. you want to mode the syntax
> table for that mode. It works when u call manually because when u are
> in that mode, it mods that mod's syntax table.

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

> hook is a good solution.

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

> e.g.
> 
> (add-hook 'w3m-mode-hook
>  (lambda ()
>   (define-key w3m-mode-map (kbd "<up>") 'previous-line) ; was w3m-
> previous-anchor. Use Shift+Tab.
>   (define-key w3m-mode-map (kbd "<down>") 'next-line) ; was w3m-next-
> anchor. Use Tab.
>   (define-key w3m-mode-map (kbd "<left>") 'backward-char) ; was w3m-
> view-previous-page. Use B.
>   (define-key w3m-mode-map (kbd "<right>") 'forward-char) ; was w3m-
> view-this-url. Use Enter.
> ))
> 
> you want to find the syntax table name for that mode to modify.

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.

-- 
Kevin Rodgers
Denver, Colorado, USA





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: c-mode and underscore
       [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
  1 sibling, 0 replies; 7+ messages in thread
From: Xah Lee @ 2009-07-08 10:44 UTC (permalink / raw)
  To: help-gnu-emacs

On Jul 7, 10:40 pm, Kevin Rodgers <kevin.d.rodg...@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.
>
> > your code mod the global syntax table. you want to mode the syntax
> > table for that mode. It works when u call manually because when u are
> > in that mode, it mods that mod's syntax table.
>
> How could c-mode-syntax-table refer to the current (not global) syntax
> table?
>
> > hook is a good solution.
>
> 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.")
>
> > e.g.
>
> > (add-hook 'w3m-mode-hook
> >  (lambda ()
> >   (define-key w3m-mode-map (kbd "<up>") 'previous-line) ; was w3m-
> > previous-anchor. Use Shift+Tab.
> >   (define-key w3m-mode-map (kbd "<down>") 'next-line) ; was w3m-next-
> > anchor. Use Tab.
> >   (define-key w3m-mode-map (kbd "<left>") 'backward-char) ; was w3m-
> > view-previous-page. Use B.
> >   (define-key w3m-mode-map (kbd "<right>") 'forward-char) ; was w3m-
> > view-this-url. Use Enter.
> > ))
>
> > you want to find the syntax table name for that mode to modify.
>
> 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.


Thanks Kevin. I was probably wrong. Don't know this issue well. Was
trying to give a quick direction towards the solution but i was
misleading.

 Xah


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: c-mode and underscore
       [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
  2009-07-08 18:57       ` geophile
  1 sibling, 1 reply; 7+ messages in thread
From: Alan Mackenzie @ 2009-07-08 10:52 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: c-mode and underscore
  2009-07-08 10:52     ` Alan Mackenzie
@ 2009-07-08 18:57       ` geophile
  0 siblings, 0 replies; 7+ messages in thread
From: geophile @ 2009-07-08 18:57 UTC (permalink / raw)
  To: help-gnu-emacs

On Jul 8, 6:52 am, Alan Mackenzie <a...@muc.de> wrote:
> Hi, Kevin!
>
>
>
> Kevin Rodgers <kevin.d.rodg...@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.

That works perfectly, thank you.

If I want to do the same for C++ and java modes, do I simply repeat
the above code, replacing c-initialization-hook by c++-initialization-
hook and java-initialization-hook?

Jack


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2009-07-08 18:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2009-07-08 18:57       ` geophile

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.