Skip to main content

Emacs Question: Indenting Non-code

I can't figure out how to get Emacs to do what I want to do. I've tried Googling and reading the manual, but the problem is that I want to control indentation of something that isn't code and doesn't have an existing mode devoted to it.

I have a bunch of files that end in .otl that are basically outlines. They are indented using tabs. Tabs are set to 4 spaces. (Yes, I know. Setting tabs to be anything other than 8 spaces is evil.)

When I'm editing an .otl file, I want Emacs to:
  • Treat tabs as 4 spaces wide.
  • Actually insert a tab whenever I hit tab.
  • Indent newlines to the same indentation level as the line above.
I don't want these rules applied to anything other than .otl files.

Can any Emacs gurus out there help me? Thanks.

Comments

Glyph Lefkowitz said…
You'll want to write a major-mode for .otl files based on fundamental-mode.

Then you want to make indent-tabs-mode buffer-local and set to t in that mode, set tab-width to 4.

You might be able to emulate this with find-file-hook.
Thanks, Glyph. Ugh.
David Goodger said…
Adding a block like this to the end of your file does what you want:

Local Variables:
mode: indented-text
indent-tabs-mode: t
tab-width: 4
eval: (local-set-key "^M" 'newline-and-indent)
End:

There may be better ways; I'm not an expert.

This can be reworked into some elisp code for your .emacs file too, so any .otl file will trigger it. It's not much code.

Or you could try the Emacs outline mode.
Hey David,

Thanks for your help. How do I make sure those settings only apply to .otl files?
> How do I make sure those settings only apply to .otl files?

Ugh, never mind. You're saying I should put that in the files themselves.
Larry said…
Try searching for find-file-hooks; it should be possible to write a Lisp function that runs when a file is loaded.
shreevatsa said…
This is certainly easy to do -- but you should ask somewhere you are likely to find real Emacs gurus, not on your blog ;-)

For example, the following seems to work (which is what the first poster was suggesting as a hack):

(defun otl-stuff ()
(interactive)
(if (buffer-file-name)
(when (string-match "\\.otl$" buffer-file-name)
(setq tab-width 4)
(setq indent-tabs-mode t))))

(add-hook 'find-file-hook 'otl-stuff)

but it is a bad solution in many ways, and I can imagine there would be much shorter ways of doing it.