why mode isn't enabled? - emacs

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.

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

Set variable under specific mode 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.

Emacs CEDET Hook

I want to hook CEDET modes to c++ mode. I am using the following script in my .emacs file:
(add-hook 'c++-mode-hook
(lambda ()
...
(my_cedet_load)
)
)
where
(defun my_cedet_load ()
(interactive)
(semantic-mode)
(global-semantic-stickyfunc-mode t)
(global-semantic-idle-scheduler-mode t)
(global-semantic-idle-completions-mode t)
(global-semantic-highlight-edits-mode t)
)
Now, the problem is that once I open a .cpp file, the semantic-mode is enabled in all buffers. How do I only enable such mode in only .cpp files?
Semantic is a global minor mode. From semantic.el
To enable Semantic, turn on `semantic-mode', a global minor mode (M-x
semantic-mode RET, or "Source Code Parsers" from the Tools menu). To
enable it at startup, put (semantic-mode 1) in your init file.
As such when you do semantic-mode it is enabled in all buffers. You can use semantic-inhibit-functions to restrict the buffers in which semantic is activated. From the documentation
List of functions to call with no arguments before Semantic is setup.
If any of these functions returns non-nil, the current buffer is not
setup to use Semantic.
Below is an example of using this variable. it would instruct semantic to be activated only in c-mode, cc-mode and java-mode buffers
(add-to-list 'semantic-inhibit-functions
(lambda () (not (member major-mode '(java-mode c-mode c++-mode)))))
I'm guessing the key lies in the global word.
So use semantic-stickyfunc-mode instead of global-semantic-stickyfunc-mode etc.
UPDATE:
Try this:
(add-hook 'c++-mode-hook 'my-c++-hook)
(defun my-c++-hook ()
(semantic-mode 1)
(semantic-stickyfunc-mode 1)
(semantic-idle-scheduler-mode 1)
(semantic-idle-completions-mode 1)
(semantic-highlight-edits-mode 1))

Why are my mode specific .emacs hooks running for all modes?

I'm running the following code in my .emacs file:
(defun load-hooks ()
(add-hook 'after-save-hook 'my-hook))
(add-hook 'c-mode-hook 'load-hooks)
(defun my-hook () ... )
However, the content in my-hook is running on save even when I'm in a different mode. Am I missing a step?
You should use the LOCAL argument to add-hook, which will make sure that the hook only affects the current buffer:
(defun load-hooks ()
(add-hook 'after-save-hook 'my-hook nil t))
(add-hook 'c-mode-hook 'load-hooks)
(defun my-hook () ...)
I think that calling (add-hook 'after-save-hook 'my-hook) in load-hooks adds the hook to all modes. That is, once that function is called, after-save-hook is modified for every other buffer as well.
I suspect that your hook would not be run unless you open a c file. Try opening some file without having opened any c files and see if anything is run. If it isn't it just means that the function that runs for c files modifies the save hook for everything else.
Tikhon was correct about the 'after-save-hook affecting all modes - I am now relying on a check using the following functions:
(defun in-c-mode? ()
(string= (current-major-mode) "c-mode"))
(defun current-major-mode ()
(with-current-buffer (current-buffer) major-mode))

Emacs: How to disable specific global mode in major-mode hook?

I use global-autopair-mode, but with Ruby the ruby-electric-mode provides better autopairing.
I'm a n00b to Emacs and Lisp, so I would have thought you could do something like:
(add-hook 'ruby-mode-hook
(lambda ()
(autopair-mode nil)
(ruby-electric-mode t))
This however doesn't work. Is there a way to disable global modes within a major-mode hook?
Currently I'm using the last solution posted here, but it's not very neat and clean.
autopair mode honors autopair-dont-activate, used like so:
(add-hook 'ruby-mode-hook
(lambda ()
(setq autopair-dont-activate t)
(ruby-electric-mode t))
Try replacing (autopair-mode nil) with (autopair-mode -1).