Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload an emacs major mode?

I have a buffer in some major-mode, and in another buffer the mode file itself (*.el). After I edit the *.el file I want to see the changes reflected in the first buffer, without restarting emacs. I tried to run the -mode function but it didn't change the buffer. Thanks

like image 708
yuk Avatar asked Dec 05 '10 16:12

yuk


People also ask

How do I reload Emacs?

You can use the command load-file ( M-x load-file , then press return twice to accept the default filename, which is the current file being edited). You can also just move the point to the end of any sexp and press C-x C-e to execute just that sexp.

How do I change major mode in Emacs?

You can change to a different major mode by executing its command. For TextMode (indicated by “Text” in the mode-line), for example, do 'M-x text-mode' . You can use 'C-h m' for help on the current major mode, and 'C-h b' to see all the bindings in effect.

What is the default mode of Emacs?

The standard default value is fundamental-mode . If the default value is nil , then whenever Emacs creates a new buffer via a command such as C-x b ( switch-to-buffer ), the new buffer is put in the major mode of the previously current buffer.


3 Answers

If your mode provides a feature (as it should!) using (provide 'foo-mode) then you can

M-x unload-feature RET foo-mode RET

and then load the mode again as normal (using foo-mode if you have an appropriate autoload, or using load-library or load-file otherwise).

like image 91
Gareth Rees Avatar answered Oct 01 '22 10:10

Gareth Rees


M-x load-file your-mode.el

or

M-x eval-buffer

Then toggle the behavior on and off in the buffer, presumably by doing

M-x your-mode
M-x your-mode

Or, if your mode recognizes the prefix argument

C-u 1 M-x your-mode

Note: When you're loading a file, defvar doesn't override existing values, so if you change the values in the call to defvar, you'll need to evaluate those specifically, either by doing C-M-x when your cursor is in the devfar expression, or using M-x : and typing in the expression. See this page for documentation on evaluating lisp in Emacs.

like image 22
Trey Jackson Avatar answered Oct 01 '22 11:10

Trey Jackson


When you edit the source of a mode, you have to make sure that you evaluate the functions you change -- saving them to file alone will not be enough, because internally Emacs will still use the old code.

For instance, you could jump to the end of the function definition you work on with M-C-e and evaluate the function with C-x C-e. From that point on, Emacs will then use the current definition.

This works for mode definition, too, but often times invoking a mode with M-x mode-name is implemented as a toggle: you call it once, it activates the mode, you call it again, it de-activates the mode. So you may have to do M-x mode-name twice.

like image 1
Thomas Avatar answered Oct 01 '22 12:10

Thomas