Discussion:
major-mode hook configuration affects other buffers
(too old to reply)
a***@debesys.net
2012-12-13 17:16:15 UTC
Permalink
Hello -

Let me start by saying I'm very new to emacs.

I'm attempting to create customizations for major-modes. While my settings are functioning correctly, I'm observing that when I open a new buffer, that buffers major-mode customization is being applied to other buffers of a different type.

For instance, if I open a file named 'Makefile', makefile-mode is used and my customizations are applied. If I then open another file such as 'test.c', c-mode is used but customizations from makefile-mode are merged with customizations from c-mode.

The relevant portions of my .emacs file can be seen below:

----

(defun c-mode-settings ()
(c-set-style "bsd")
(set-buffer-file-coding-system 'utf-8-unix)
(show-paren-mode 1)

(setq c-basic-offset 4)
(setq tab-width 4)
(setq indent-tabs-mode nil)
(setq c-tab-always-indent t)
(setq require-final-newline t)
)

(defun makefile-mode-settings ()
(setq whitespace-style '(tabs spaces space-mark tab-mark face lines-tail))
(whitespace-mode t)
(show-paren-mode 1)

(setq tab-width 4)
(setq require-final-newline t)
)

(add-hook 'c-mode-hook 'c-mode-settings)
(add-hook 'makefile-mode-hook 'makefile-mode-settings)

----


How can I keep these mode hooks from affecting other buffers in different modes?

Thanks!
Andrew
Lowell Gilbert
2012-12-15 20:35:44 UTC
Permalink
Post by a***@debesys.net
Hello -
Let me start by saying I'm very new to emacs.
I'm attempting to create customizations for major-modes. While my
settings are functioning correctly, I'm observing that when I open a
new buffer, that buffers major-mode customization is being applied to
other buffers of a different type.
For instance, if I open a file named 'Makefile', makefile-mode is used
and my customizations are applied. If I then open another file such
as 'test.c', c-mode is used but customizations from makefile-mode are
merged with customizations from c-mode.
----
(defun c-mode-settings ()
(c-set-style "bsd")
(set-buffer-file-coding-system 'utf-8-unix)
(show-paren-mode 1)
(setq c-basic-offset 4)
(setq tab-width 4)
(setq indent-tabs-mode nil)
(setq c-tab-always-indent t)
(setq require-final-newline t)
)
(defun makefile-mode-settings ()
(setq whitespace-style '(tabs spaces space-mark tab-mark face lines-tail))
(whitespace-mode t)
(show-paren-mode 1)
(setq tab-width 4)
(setq require-final-newline t)
)
(add-hook 'c-mode-hook 'c-mode-settings)
(add-hook 'makefile-mode-hook 'makefile-mode-settings)
----
How can I keep these mode hooks from affecting other buffers in different modes?
Use the command make-variable-buffer-local before setting a variable
that you don't want to change globally. Some variables are buffer-local
already, because a mode author thought they were likely to be desired
that way. c-basic-offset and tab-width are such variables already, so
you probably don't have problems with them. You don't mention what
settings you *are* having problems with, but I think this is probably
enough already for you to work with.

Example:
(defun makefile-mode-settings ()
(make-variable-buffer-local 'whitespace-style)
(setq whitespace-style '(tabs spaces space-mark tab-mark face lines-tail)))

Good luck.
--
Lowell Gilbert, embedded/networking software engineer
http://be-well.ilk.org/~lowell/
Michael Heerdegen
2012-12-17 18:38:59 UTC
Permalink
Post by Lowell Gilbert
Post by a***@debesys.net
How can I keep these mode hooks from affecting other buffers in different modes?
Use the command make-variable-buffer-local before setting a variable
that you don't want to change globally. Some variables are buffer-local
already, because a mode author thought they were likely to be desired
that way. c-basic-offset and tab-width are such variables already, so
you probably don't have problems with them. You don't mention what
settings you *are* having problems with, but I think this is probably
enough already for you to work with.
(defun makefile-mode-settings ()
(make-variable-buffer-local 'whitespace-style)
(setq whitespace-style '(tabs spaces space-mark tab-mark face lines-tail)))
Let me add that in most cases, it is not necessary (or even dangerous)
to make a variable buffer local in all buffers by default with
`make-variable-buffer-local'. Better use `make-local-variable'. In the
example above:

(defun makefile-mode-settings ()
(set (make-local-variable 'whitespace-style)
'(tabs spaces space-mark tab-mark face lines-tail)))

See the elisp manual node "Buffer-Local Variables".


Regards,

Michael.

Loading...