Set variable under specific mode emacs - emacs

Looking to set a variable under latex mode. The idea is that the value set under latex mode will override the value of the same variable set in the customise section. I am very new to emacs so these are my attempts:
(add-hook 'LaTeX-mode-hook '(setq line-move-visual t))
(add-hook 'latex-mode-hook (lambda () (setq line-move-visual t)))
Why do these not work? What should I do instead?
Clarification: looking to set the variable (setq line-move-visual t) as I have it as (setq line-move-visual nil) for all other files

If you just setq the variable in your LaTeX-mode-hook it will also have an effect on any other open buffer. It is possible to make the change only effect the current buffer:
(add-hook 'LaTeX-mode-hook
(lambda ()
(make-local-variable 'line-move-visual)
(setq line-move-visual nil)))
Also, please note that the hook for the default mode for LaTeX in Emacs is called latex-mode-hook but the hook when you are using the (far superior) AUCTeX is called LaTeX-mode-hook
EDIT: Changed make-variable-buffer-local to make-local-variable. See comments to this answer.

Related

Emacs Elisp Overriding Default Value

I have several abbrev defined that I was accessible everywhere except in latex mode. I defined
(setq-default abbrev-mode t)
(add-hook 'latex-mode-hook (lambda () (abbrev-mode -1)))
But whenever I open a latex file it still has abbrev mode enabled. What's going on?
Never worked with latex before, but for me the following works fine:
(setq auto-mode-alist (cons '("\\.lat\\'" . latex-mode) auto-mode-alist))
(setq-default abbrev-mode t)
(add-hook 'latex-mode-hook (lambda () (abbrev-mode -1)))
M-x abbrev-mode
%Abbrev mode enabled in current buffer
Please make sure the emacs recognized your file as a latex file, the first line I wrote should do the trick.
The reason was that AUCTex uses LaTeX-mode-hook. Thanks to stefan in the comment for pointing that out

fill-column setting in tuareg mode not working

I have following setting in ~/.emacs
(defun my-fillcol-hook ()
(longlines-mode t)
(setq-default fill-column 80))
(add-hook 'c-mode-hook 'my-fillcol-hook)
(add-hook 'c++-mode-hook 'my-fillcol-hook)
(add-hook 'tuareg-mode-hook 'my-fillcol-hook)
The fill-column is set as expected in C-mode and C++-mode but not in tuareg-mode. I am not able to find out why fill-column is not getting set in tuareg-mode. Thanks in advance for the help.
setq-default changes the global value, whereas tuareg-mode apparently sets a buffer-local value which takes precedence. IOW you want to use setq-local or just setq.

How to highlight tab characters for files from specific project in Emacs

I have this code to highlight tab characters and want to disable them using project-specifics macro:
(require 'highlight-chars)
(make-variable-buffer-local 'prevent-highlight-tabs)
(setq highlight-chars-disable '(term-mode erc-mode fundamental-mode))
(setq-default prevent-highlight-tabs nil)
(add-hook 'font-lock-mode-hook
(lambda()
(message "lock")
(when (and (null (memql major-mode highlight-chars-disable))
(not prevent-highlight-tabs))
(message "%s" prevent-highlight-tabs)
(hc-highlight-tabs))))
(custom-set-faces '(hc-tab ((t (:background "red")))))
and project-specifics is a macro that define add find-file-hook and dired-after-readin-hook from this question
(project-specifics "projects/test"
(message "specific")
(setq prevent-highlight-tabs t)
(setq indent-tabs-mode t))
What I wanted to do is disable red tabs (I want them because in most projects I want only spaces, and want to see tabs) for files in project/test, but I have a problem because the code from font-lock-mode-hook is executing before project-specifics (find-file-hook), and prevent-highlight-tabs is always nil in font-lock-mode-hook. Why is that, and how to fix it?
I suggest that you place your settings in .dir-locals.el as described in Per-Directory Local Variables.
What you are doing seems convoluted.
And it is partly undefined (here) - e.g., what is the major mode highlight-chars-disable? That is not defined in library highlight-chars.el, and it doesn't sound like something that would be a good candidate for a major mode.
See the Commentary of library highlight-chars.el for suggestions.
Something like this, perhaps (and you would put property prevent-highlight-tabs on any major-mode symbols you like:
(add-hook 'font-lock-mode-hook
(lambda () (unless (get major-mode 'prevent-highlight-tabs)
(hc-highlight-tabs))))
Or something like this (from the Commentary):
(add-hook 'change-major-mode-hook
(lambda ()
(add-hook 'font-lock-mode-hook 'hc-highlight-tabs)))
(add-hook 'after-change-major-mode-hook
(lambda ()
(when (eq major-mode 'THE-MODE)
(remove-hook 'font-lock-mode-hook 'hc-highlight-tabs)
(hc-dont-highlight-tabs)))
'APPEND)

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

why mode isn't enabled?

I've got an emacs configuration file whatever.el :
(abbrev-mode +1)
(provide 'whatever)
and in my init.el :
(require 'whatever)
but when i start emacs, abbrev-mode isn't enabled. why ?
thank you
Quoting from http://emacswiki.org/emacs/AbbrevMode:
You can also put the following in your ~/.emacs file if you want it
always on:
(setq default-abbrev-mode t)
If you only want it on in text and derived modes, you could do
something like this:
(add-hook 'text-mode-hook (lambda () (abbrev-mode 1)))
For multiple modes, use something like the following:
(dolist (hook '(erc-mode-hook
emacs-lisp-mode-hook
text-mode-hook))
(add-hook hook (lambda () (abbrev-mode 1))))
Abbrev-mode is enabled per-buffer.
One way is to create a hook function that you could add to the major mode hooks you will want to use it in.
For example:
(defun my-enable-abbrev-mode ()
(abbrev-mode 1))
(add-hook 'c-mode-hook 'my-enable-abbrev-hook)
(add-hook 'java-mode-hook 'my-enable-abbrev-hook)
Another approach is to use change-major-mode-hook.
While others explained how to get what you presumably want, I'll just point out that w.r.t to your actual question ("Why?"), the reason is simple: abbrev-mode is a buffer-local minor-mode, so when you run (abbrev-mode +1) at startup it will just enable abbrev-mode in the buffer that happens to be current during evaluation of the ~/.emacs (typically scratch) but not in subsequent buffers.