Discussion:
Standalone CC-Mode doesn't run prog-mode-hook
Philipp Stephani
2016-10-25 11:37:50 UTC
Permalink
Hi,

if you start a current Emacs with emacs -Q and then run

(with-temp-buffer
(let ((calls 0)
(prog-mode-hook prog-mode-hook))
(add-hook 'prog-mode-hook (lambda () (cl-incf calls)))
(java-mode)
(cl-assert (= calls 1))))

the assertion succeeds (i.e. doesn't trigger). However, when I tried this
with the CC-Mode from Sourceforge, it didn't work. I downloaded
http://downloads.sourceforge.net/project/cc-mode/cc-mode/5.33/cc-mode-5.33.tar.gz
and unpacked it into a temporary directory (/tmp/cc-mode-5.33). After
starting Emacs with emacs -Q -L /tmp/cc-mode-5.33 and running above code,
the assertion triggers because prog-mode-hook hasn't been called. I've
tried this before and after compiling the CC-Mode files in
/tmp/cc-mode-5.33, the result is the same. Am I doing something wrong, or
is this a new bug?

Thanks,
Philipp
Philipp Stephani
2016-10-24 12:37:22 UTC
Permalink
Hi,

if you start a current Emacs with emacs -Q and then run

(with-temp-buffer
(let ((calls 0)
(prog-mode-hook prog-mode-hook))
(add-hook 'prog-mode-hook (lambda () (cl-incf calls)))
(java-mode)
(cl-assert (= calls 1))))

the assertion succeeds (i.e. doesn't trigger). However, when I tried this
with the CC-Mode from Sourceforge, it didn't work. I downloaded
http://downloads.sourceforge.net/project/cc-mode/cc-mode/5.33/cc-mode-5.33.tar.gz
and
unpacked it into a temporary directory (/tmp/cc-mode-5.33). After starting
Emacs with emacs -Q -L /tmp/cc-mode-5.33 and running above code, the
assertion triggers because prog-mode-hook hasn't been called. I've tried
this before and after compiling the CC-Mode files in /tmp/cc-mode-5.33, the
result is the same. Am I doing something wrong, or is this a new bug?

Thanks,
Philipp
Alan Mackenzie
2016-10-29 13:38:56 UTC
Permalink
Hello, Philipp.
Post by Philipp Stephani
Hi,
if you start a current Emacs with emacs -Q and then run
(with-temp-buffer
(let ((calls 0)
(prog-mode-hook prog-mode-hook))
(add-hook 'prog-mode-hook (lambda () (cl-incf calls)))
(java-mode)
(cl-assert (= calls 1))))
the assertion succeeds (i.e. doesn't trigger). However, when I tried this
with the CC-Mode from Sourceforge, it didn't work. I downloaded
http://downloads.sourceforge.net/project/cc-mode/cc-mode/5.33/cc-mode-5.33.tar.gz
and unpacked it into a temporary directory (/tmp/cc-mode-5.33). After
starting Emacs with emacs -Q -L /tmp/cc-mode-5.33 and running above code,
the assertion triggers because prog-mode-hook hasn't been called. I've
tried this before and after compiling the CC-Mode files in
/tmp/cc-mode-5.33, the result is the same. Am I doing something wrong, or
is this a new bug?
It isn't a bug. Standalone CC Mode is not derived from prog-mode,
therefore prog-mode-hook doesn't get called. Why should it?

There are several reasons why this is the case. prog-mode in Emacs is
relatively new, and doesn't exist in XEmacs. When Emacs's CC Mode was
converted to derive from prog-mode, several problems were brushed over,
and later turned up as bugs (such as mode hooks being called twice, and
AWK Mode's syntax table (or it might have been its key map) not being
initialised). It seems that these problems have now (hopefully) been
solved.
Post by Philipp Stephani
Thanks,
Philipp
--
Alan Mackenzie (Nuremberg, Germany).
Philipp Stephani
2016-10-31 14:38:30 UTC
Permalink
Post by Alan Mackenzie
Hello, Philipp.
Post by Philipp Stephani
Hi,
if you start a current Emacs with emacs -Q and then run
(with-temp-buffer
(let ((calls 0)
(prog-mode-hook prog-mode-hook))
(add-hook 'prog-mode-hook (lambda () (cl-incf calls)))
(java-mode)
(cl-assert (= calls 1))))
the assertion succeeds (i.e. doesn't trigger). However, when I tried this
with the CC-Mode from Sourceforge, it didn't work. I downloaded
http://downloads.sourceforge.net/project/cc-mode/cc-mode/5.33/cc-mode-5.33.tar.gz
Post by Philipp Stephani
and unpacked it into a temporary directory (/tmp/cc-mode-5.33). After
starting Emacs with emacs -Q -L /tmp/cc-mode-5.33 and running above code,
the assertion triggers because prog-mode-hook hasn't been called. I've
tried this before and after compiling the CC-Mode files in
/tmp/cc-mode-5.33, the result is the same. Am I doing something wrong, or
is this a new bug?
It isn't a bug. Standalone CC Mode is not derived from prog-mode,
therefore prog-mode-hook doesn't get called. Why should it?
Sorry for the confusion, with "Standalone CC-Mode" I mean the CC-Mode
libraries downloaded from Sourceforge as a whole, not c-standalone-mode.
Post by Alan Mackenzie
There are several reasons why this is the case. prog-mode in Emacs is
relatively new, and doesn't exist in XEmacs. When Emacs's CC Mode was
converted to derive from prog-mode, several problems were brushed over,
and later turned up as bugs (such as mode hooks being called twice, and
AWK Mode's syntax table (or it might have been its key map) not being
initialised). It seems that these problems have now (hopefully) been
solved.
I'm still quite confused by the behavior differences. Both the CC-Mode
shipped with Emacs and the CC-Mode from SourceFourge announce themselves as
"version 5.33". However, these seemingly identical versions exhibit a large
number of differences. The one that's relevant here is that Emacs's CC-Mode
defines java-mode with
(define-derived-mode java-mode prog-mode "Java" ...
while SourceForge's CC-Mode uses
(defun java-mode () ...
Why are there apparently two separate forks of CC-Mode? Which one is
newer/preferable? Is the CC-Mode from SourceForge supposed to be usable as
a drop-in replacement for the one shipped with Emacs?
Thanks.
Philipp Stephani
2016-11-02 14:20:57 UTC
Permalink
Post by Philipp Stephani
I'm still quite confused by the behavior differences. Both the CC-Mode
shipped with Emacs and the CC-Mode from SourceFourge announce themselves as
"version 5.33". However, these seemingly identical versions exhibit a large
number of differences. The one that's relevant here is that Emacs's CC-Mode
defines java-mode with
(define-derived-mode java-mode prog-mode "Java" ...
while SourceForge's CC-Mode uses
(defun java-mode () ...
The behavior of the CC-Mode shipped with Emacs is definitely the correct
one. See
https://www.gnu.org/software/emacs/manual/html_node/elisp/Derived-Modes.html
:
"The recommended way to define a new major mode is to derive it from an
existing one using define-derived-mode. If there is no closely related
mode, you should inherit from either text-mode, special-mode, or
prog-mode."
Is there any reason why the CC-Mode from SourceForge doesn't follow this
recommendation?
Alan Mackenzie
2016-11-02 21:34:03 UTC
Permalink
Hello, Philipp.
Post by Philipp Stephani
Post by Philipp Stephani
I'm still quite confused by the behavior differences. Both the CC-Mode
shipped with Emacs and the CC-Mode from SourceFourge announce themselves as
"version 5.33". However, these seemingly identical versions exhibit a large
number of differences. The one that's relevant here is that Emacs's CC-Mode
defines java-mode with
(define-derived-mode java-mode prog-mode "Java" ...
while SourceForge's CC-Mode uses
(defun java-mode () ...
The behavior of the CC-Mode shipped with Emacs is definitely the correct
one.
They are both correct.
Post by Philipp Stephani
See
https://www.gnu.org/software/emacs/manual/html_node/elisp/Derived-Modes.html
"The recommended way to define a new major mode is to derive it from an
existing one using define-derived-mode. If there is no closely related
mode, you should inherit from either text-mode, special-mode, or
prog-mode."
As it says, that's just a recommendation, nothing more.
Post by Philipp Stephani
Is there any reason why the CC-Mode from SourceForge doesn't follow this
recommendation?
Yes. To be able to work with XEmacs and older versions of Emacs which
lack prog-mode. It is only recently that define-derived-mode has
acquired features needed for bug-free support of CC Mode.
--
Alan Mackenzie (Nuremberg, Germany).
Alan Mackenzie
2016-11-02 21:21:17 UTC
Permalink
Hello, Philipp.
Post by Philipp Stephani
Post by Alan Mackenzie
Hello, Philipp.
Post by Philipp Stephani
Hi,
if you start a current Emacs with emacs -Q and then run
(with-temp-buffer
(let ((calls 0)
(prog-mode-hook prog-mode-hook))
(add-hook 'prog-mode-hook (lambda () (cl-incf calls)))
(java-mode)
(cl-assert (= calls 1))))
the assertion succeeds (i.e. doesn't trigger). However, when I tried this
with the CC-Mode from Sourceforge, it didn't work. I downloaded
http://downloads.sourceforge.net/project/cc-mode/cc-mode/5.33/cc-mode-5.33.tar.gz
Post by Philipp Stephani
and unpacked it into a temporary directory (/tmp/cc-mode-5.33). After
starting Emacs with emacs -Q -L /tmp/cc-mode-5.33 and running above code,
the assertion triggers because prog-mode-hook hasn't been called. I've
tried this before and after compiling the CC-Mode files in
/tmp/cc-mode-5.33, the result is the same. Am I doing something wrong, or
is this a new bug?
It isn't a bug. Standalone CC Mode is not derived from prog-mode,
therefore prog-mode-hook doesn't get called. Why should it?
Sorry for the confusion, with "Standalone CC-Mode" I mean the CC-Mode
libraries downloaded from Sourceforge as a whole, not c-standalone-mode.
Yes, understood.
Post by Philipp Stephani
Post by Alan Mackenzie
There are several reasons why this is the case. prog-mode in Emacs is
relatively new, and doesn't exist in XEmacs. When Emacs's CC Mode was
converted to derive from prog-mode, several problems were brushed over,
and later turned up as bugs (such as mode hooks being called twice, and
AWK Mode's syntax table (or it might have been its key map) not being
initialised). It seems that these problems have now (hopefully) been
solved.
I'm still quite confused by the behavior differences. Both the CC-Mode
shipped with Emacs and the CC-Mode from SourceFourge announce themselves as
"version 5.33".
That's a bug. The version in Emacs 25.1 is quite a long way behind that
in standalone CC Mode 5.33. I changed the version number for Emacs 25
quite a long time ago and forgot about it until it was too late to do
anything about it. I'm going to change that version number to something
like 5.32.90 for Emacs 25.2.
Post by Philipp Stephani
However, these seemingly identical versions exhibit a large
number of differences.
Yes.
Post by Philipp Stephani
The one that's relevant here is that Emacs's CC-Mode defines java-mode with
(define-derived-mode java-mode prog-mode "Java" ...
while SourceForge's CC-Mode uses
(defun java-mode () ...
That difference shouldn't actually be that significant.
Post by Philipp Stephani
Why are there apparently two separate forks of CC-Mode?
Various people in Emacs have been changing the Emacs version of CC Mode
for quite a long time. I haven't had the necessary motivation to try and
assert control over it, except sometimes. Most of these changes are not,
of themselves, bad.
Post by Philipp Stephani
Which one is newer/preferable?
Standalone CC Mode 5.33 is newer than the CC Mode in Emacs 25. The one
in the Emacs master branch is very close to standalone 5.33.
Post by Philipp Stephani
Is the CC-Mode from SourceForge supposed to be usable as a drop-in
replacement for the one shipped with Emacs? Thanks.
The two CC Modes were originally intended to have the same functionality,
but have drifted apart a little. Standalone CC Mode 5.33 should work
without problem in Emacs 25.x, yes.
--
Alan Mackenzie (Nuremberg, Germany).
Philipp Stephani
2016-11-03 13:16:04 UTC
Permalink
Post by Alan Mackenzie
Hello, Philipp.
Post by Philipp Stephani
Post by Alan Mackenzie
Hello, P
I'm still quite confused by the behavior differences. Both the CC-Mode
shipped with Emacs and the CC-Mode from SourceFourge announce themselves
as
Post by Philipp Stephani
"version 5.33".
That's a bug. The version in Emacs 25.1 is quite a long way behind that
in standalone CC Mode 5.33. I changed the version number for Emacs 25
quite a long time ago and forgot about it until it was too late to do
anything about it. I'm going to change that version number to something
like 5.32.90 for Emacs 25.2.
OK, thanks for the explanation. What about the version in the master branch
of the Emacs repo? That has some changes from September.
Post by Alan Mackenzie
Post by Philipp Stephani
The one that's relevant here is that Emacs's CC-Mode defines java-mode
with
Post by Philipp Stephani
(define-derived-mode java-mode prog-mode "Java" ...
while SourceForge's CC-Mode uses
(defun java-mode () ...
That difference shouldn't actually be that significant.
Well, it is significant because the modes from SourceForge don't inherit
from prog-mode. That's a quite significant user-visible difference. Users
are accustomed to put functions into prog-mode-hook, expecting that they
get called whenever a mode to edit programming languages is selected.
I'm not so much concerned about implementation differences (though they
make CC-Mode harder to maintain and understand), but about differences in
observable behavior.
Post by Alan Mackenzie
Post by Philipp Stephani
Which one is newer/preferable?
Standalone CC Mode 5.33 is newer than the CC Mode in Emacs 25. The one
in the Emacs master branch is very close to standalone 5.33.
Post by Philipp Stephani
Is the CC-Mode from SourceForge supposed to be usable as a drop-in
replacement for the one shipped with Emacs? Thanks.
The two CC Modes were originally intended to have the same functionality,
but have drifted apart a little. Standalone CC Mode 5.33 should work
without problem in Emacs 25.x, yes.
So could you maybe try to reconcile the observable behavior differences,
e.g. have SourceForge CC-Mode call prog-mode-hook, even if it doesn't use
`define-derived-mode'? Thanks!
Alan Mackenzie
2016-11-06 22:05:32 UTC
Permalink
Hello, Philipp.
Post by Philipp Stephani
Post by Alan Mackenzie
Hello, Philipp.
Post by Philipp Stephani
Post by Alan Mackenzie
Hello, P
I'm still quite confused by the behavior differences. Both the
CC-Mode shipped with Emacs and the CC-Mode from SourceFourge
announce themselves as "version 5.33".
That's a bug. The version in Emacs 25.1 is quite a long way behind that
in standalone CC Mode 5.33. I changed the version number for Emacs 25
quite a long time ago and forgot about it until it was too late to do
anything about it. I'm going to change that version number to something
like 5.32.90 for Emacs 25.2.
OK, thanks for the explanation. What about the version in the master branch
of the Emacs repo? That has some changes from September.
Yes, it does. I think it would be better not to change its version
number yet, because that might just repeat the above error.
Post by Philipp Stephani
Post by Alan Mackenzie
Post by Philipp Stephani
The one that's relevant here is that Emacs's CC-Mode defines java-mode with
(define-derived-mode java-mode prog-mode "Java" ...
while SourceForge's CC-Mode uses
(defun java-mode () ...
That difference shouldn't actually be that significant.
Well, it is significant because the modes from SourceForge don't inherit
from prog-mode. That's a quite significant user-visible difference. Users
are accustomed to put functions into prog-mode-hook, expecting that they
get called whenever a mode to edit programming languages is selected.
I've thought about that over the weekend. I think that's user confusion.
prog-mode-hook is called not for programming language modes, but for
prog-mode itself and modes derived from it. It doesn't help that the
"Major Modes" page in the Emacs manual falsely states that "... all
programming language modes run `prog-mode-hook', prior to running their
own mode hooks.". I'm going to raise a bug report for this.

The fact that many programming lanuguage modes are derived from prog-mode
is an internal implementation detail, not a user level feature, and users
taking advantage of such things are liable to get into trouble.
Post by Philipp Stephani
I'm not so much concerned about implementation differences (though they
make CC-Mode harder to maintain and understand), but about differences in
observable behavior.
As I say, this "observable behaviour" is merely caused by an internal
implementation decision, and shouldn't be relied upon by users.
Post by Philipp Stephani
Post by Alan Mackenzie
Post by Philipp Stephani
Which one is newer/preferable?
Standalone CC Mode 5.33 is newer than the CC Mode in Emacs 25. The one
in the Emacs master branch is very close to standalone 5.33.
Post by Philipp Stephani
Is the CC-Mode from SourceForge supposed to be usable as a drop-in
replacement for the one shipped with Emacs? Thanks.
The two CC Modes were originally intended to have the same functionality,
but have drifted apart a little. Standalone CC Mode 5.33 should work
without problem in Emacs 25.x, yes.
So could you maybe try to reconcile the observable behavior differences,
e.g. have SourceForge CC-Mode call prog-mode-hook, even if it doesn't use
`define-derived-mode'? Thanks!
No. prog-mode-hook is the hook for prog-mode, and to call it in other
circumstances would violate the mode mechanism, and might not even be
safe (e.g., something in a prog-mode hook might be dependant upon
prog-mode having been run.)
--
Alan Mackenzie (Nuremberg, Germany).
Philipp Stephani
2017-04-22 16:19:42 UTC
Permalink
Post by Alan Mackenzie
Hello, Philipp.
Post by Philipp Stephani
Post by Alan Mackenzie
Hello, Philipp.
Post by Philipp Stephani
Post by Alan Mackenzie
Hello, P
I'm still quite confused by the behavior differences. Both the
CC-Mode shipped with Emacs and the CC-Mode from SourceFourge
announce themselves as "version 5.33".
That's a bug. The version in Emacs 25.1 is quite a long way behind
that
Post by Philipp Stephani
Post by Alan Mackenzie
in standalone CC Mode 5.33. I changed the version number for Emacs 25
quite a long time ago and forgot about it until it was too late to do
anything about it. I'm going to change that version number to
something
Post by Philipp Stephani
Post by Alan Mackenzie
like 5.32.90 for Emacs 25.2.
OK, thanks for the explanation. What about the version in the master
branch
Post by Philipp Stephani
of the Emacs repo? That has some changes from September.
Yes, it does. I think it would be better not to change its version
number yet, because that might just repeat the above error.
Post by Philipp Stephani
Post by Alan Mackenzie
Post by Philipp Stephani
The one that's relevant here is that Emacs's CC-Mode defines
java-mode
Post by Philipp Stephani
Post by Alan Mackenzie
Post by Philipp Stephani
with
(define-derived-mode java-mode prog-mode "Java" ...
while SourceForge's CC-Mode uses
(defun java-mode () ...
That difference shouldn't actually be that significant.
Well, it is significant because the modes from SourceForge don't inherit
from prog-mode. That's a quite significant user-visible difference. Users
are accustomed to put functions into prog-mode-hook, expecting that they
get called whenever a mode to edit programming languages is selected.
I've thought about that over the weekend. I think that's user confusion.
prog-mode-hook is called not for programming language modes, but for
prog-mode itself and modes derived from it. It doesn't help that the
"Major Modes" page in the Emacs manual falsely states that "... all
programming language modes run `prog-mode-hook', prior to running their
own mode hooks.". I'm going to raise a bug report for this.
The fact that many programming lanuguage modes are derived from prog-mode
is an internal implementation detail, not a user level feature, and users
taking advantage of such things are liable to get into trouble.
Post by Philipp Stephani
I'm not so much concerned about implementation differences (though they
make CC-Mode harder to maintain and understand), but about differences in
observable behavior.
As I say, this "observable behaviour" is merely caused by an internal
implementation decision, and shouldn't be relied upon by users.
Post by Philipp Stephani
Post by Alan Mackenzie
Post by Philipp Stephani
Which one is newer/preferable?
Standalone CC Mode 5.33 is newer than the CC Mode in Emacs 25. The one
in the Emacs master branch is very close to standalone 5.33.
Post by Philipp Stephani
Is the CC-Mode from SourceForge supposed to be usable as a drop-in
replacement for the one shipped with Emacs? Thanks.
The two CC Modes were originally intended to have the same
functionality,
Post by Philipp Stephani
Post by Alan Mackenzie
but have drifted apart a little. Standalone CC Mode 5.33 should work
without problem in Emacs 25.x, yes.
So could you maybe try to reconcile the observable behavior differences,
e.g. have SourceForge CC-Mode call prog-mode-hook, even if it doesn't use
`define-derived-mode'? Thanks!
No. prog-mode-hook is the hook for prog-mode, and to call it in other
circumstances would violate the mode mechanism, and might not even be
safe (e.g., something in a prog-mode hook might be dependant upon
prog-mode having been run.)
Thanks for the explanation and for fixing the manual!

Loading...