Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Different options for different modes?

8 views
Skip to first unread message

winkerbean

unread,
Sep 23, 2011, 10:24:02 AM9/23/11
to
Hi,

Can Someone point Me in the direction of documentation which shows how
to set different options for different modes? (e.g., preserving tabs
as tabs in a ruby file while converting tabs to four ' ' characters in
a c file) I have searched thru the docs Myself and, though I may have
missed it, I did not see the information. I am using 23.3.

Thanks in advance.

Stefan Monnier

unread,
Sep 23, 2011, 3:32:30 PM9/23/11
to
The general approach is to use something like

(add-hook 'MAJOR-MODE-hook
(lambda ()
(set (make-local-variable 'VAR) VAL)))

where MAJOR-MODE is the name of the major mode, and I'll let you guess
what VAR and VAL might be.


Stefan

Philipp Haselwarter

unread,
Mar 29, 2012, 5:51:59 AM3/29/12
to help-gn...@gnu.org
to set options for certain modes emacs uses `hooks', usually following
the naming scheme `foo-mode-hook' where "foo" is the mode in question.

Check out the section on hooks in the emacs info manual:

C-h i m Emacs m Hooks

a quick example could be
#+begin_src: elisp
(defun my-ruby-coding-style nil
(setq indent-tabs-mode t))

(defun my-c-coding-style nil
(setq indent-tabs-mode nil
tab-width 4))

(add-hook 'c-mode-hook 'my-c-coding-style)
(add-hook 'ruby-mode-hook 'my-ruby-coding-style)
#+end_src

--
Philipp Haselwarter


rusi

unread,
Mar 29, 2012, 9:00:45 AM3/29/12
to
Stefan Monnier wrote:
> The general approach is to use something like
>
> (add-hook 'MAJOR-MODE-hook
> (lambda ()
> (set (make-local-variable 'VAR) VAL)))

I wonder when this is used and when this:

(eval-after-load 'MAJOR_MODE (setq VAR VAL))

Stefan Monnier

unread,
Apr 3, 2012, 8:59:26 PM4/3/12
to
>> (add-hook 'MAJOR-MODE-hook
>> (lambda ()
>> (set (make-local-variable 'VAR) VAL)))

> I wonder when this is used and when this:
> (eval-after-load 'MAJOR_MODE (setq VAR VAL))

Your code will be run once when loading the `MAJOR_MODE' package
(i.e. usually the first time `MAJOR_MODE' is used), whereas the code
I quoted is run every time `MAJOR-MODE' is enabled.

If you use Emacs for a single file at a time, the difference between the
two may be unnoticeable. But if you do use several files in the same
Emacs session (which is the normal way to use Emacs), then the
difference is important.


Stefan
0 new messages