I use this command to start emacs
$ emacs -Q c-mode-test.el
then I use C-xC-e to eval every line
(require 'cc-mode)
(add-hook 'c-mode-common-hook '(lambda () (print "hello")))
(add-hook 'c-mode-hook '(lambda () (print "hello c")))
(c-mode)
after this, the minibuffer shows
"hello"
"hello c"
"hello c"
nil
and c++-mode-hook run just the same
(add-hook 'c++-mode-hook '(lambda () (print "hello c++")))
(c++-mode)
the minibuffer
"hello"
"hello c++"
"hello c++"
nil
why it run twice or something wrong.
It seems that the language-specific hooks are getting run more than once. You can confirm that this is the case by running
(run-hooks c-mode-hook)
or(run-hooks c-mode-common-hook)
, and you'll notice that yourprint
statement only happens once.The general advice with hooks is to not be dependent on the order in which they're run -- not depending on how many times they're run seems like a natural extension there.