Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

major-mode hook configuration affects other buffers

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

like image 691
user1902144 Avatar asked Oct 05 '22 16:10

user1902144


1 Answers

You need to take into account, that some variables are becoming local to buffer when set, while some are global. Usually they have corresponding comment in their description (use C-h v var-name to get this description.

In some cases, you can force that any variable become local to buffer, using the

(set (make-local-variable 'var-name) var-value)

but you need to be careful with this.

like image 85
Alex Ott Avatar answered Oct 10 '22 02:10

Alex Ott