-1

I'm using Emacs 23.1.1 on Ubuntu with Emacs starter kit. I primarily work in the lua-mode.

Is there a way to stop Emacs being so smart about indentation? I'm used to the dumb editors, and press all the required keys manually.

I want to use two spaces per indent, tabs-to-spaces.

When I press RETURN, the new line indentation must match the previous line.

When I press TAB on the leading whitespace, the line contents must be indented by one indentation unit.

When I press TAB on the beginning of empty line, the cursor must move one indentation unit to the right.

Oh, and I'd like to get soft word wrap on 80th column and trim-trailing-spaces on save as well.

Update:

(Would put this in a comment, but it needs formatting)

If I use Thomas's solution, auto-indent on RETURN is "fixed", but TAB still indents weirdly:

local run = function(...)
           x

"x" marks the spot where cursor appears after I type the first line and hit RETURN, TAB.

Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160
  • 6
    Why not use another editor? Why use emacs if it's not doing what you want by default? – Philip Potter Jan 09 '11 at 07:08
  • 1
    Why not spend a bit getting used to a smarter editor? It's certainly possible to get it to do what you want, but I find the tabbing behavior to be one of the things emacs gets quite right out of the box. Give it a bit of time and realize that it's just making it easier to do what you'd be doing anyway. – Dustin Jan 09 '11 at 07:39
  • @Philip: Because I'm curious. – Alexander Gladysh Jan 09 '11 at 14:49
  • @Dustin: What emacs does does not match the coding guidelines that I use. – Alexander Gladysh Jan 09 '11 at 14:50
  • Alexander, you can adjust the lua-specific mode to meet your coding standards. That's not the problem you're asking to be fixed. Your question has nothing to do with how far indentation is, but with whether it happens or not at particular locations. Tell emacs where you want the indentation to go and let it actually do the work for you. – Dustin Jan 10 '11 at 02:43
  • @Dustin: I wish I knew how to do this. :-) http://stackoverflow.com/questions/4643206/how-to-configure-indentation-in-emacs-lua-mode/4643343#4643343 – Alexander Gladysh Jan 10 '11 at 04:09

2 Answers2

3

Emacs has a concept of modes, which means that depending on what type of file you're editing it provides special functionality that is useful for that file. Every buffer has one major mode associated and optionally a number of minor modes.

Indentation is one of the things that is typically mode-dependent. That is, you may have to configure indentation separately for every major-mode, because otherwise when you load a new file, its associated major mode may override your indentation settings. It's possible though to write a function that configures indentation and set up Emacs in a way that the function is invoked whenever a new major-mode is started.

In order to realize the settings you want, you'll need to run a few lines of elisp code. (Unfortunately your description of what should happen when you hit TAB leaves out some details, I've implemented the simplest version I could think of below -- if it's not what you want, that can be changed, of course.)

Put the following code in the file named .emacs in your home directory (~):

(setq-default indent-tabs-mode nil) ; use spaces for indentation

(defvar my-indentation-width 2
  "The number of spaces I prefer for line indentation.")

(defun my-enter ()
  "Inserts a newline character then indents the new line just
like the previous line"
  (interactive)
  (newline)
  (indent-relative-maybe))

(defun my-indent ()
  "When point is on leading white-space of a non-empty line, the
line is indented `my-indentation-width' spaces. If point is at
the beginning of an empty line, inserts `my-indentation-width'
spaces."
  (interactive)
  (insert (make-string my-indentation-width ? )))

(defun my-indentation-setup ()
  "Binds RETURN to the function `my-enter' and TAB to call
`my-indent'"
  (local-set-key "\r" 'my-enter)
  (setq indent-line-function 'my-indent))

(defun delete-trailing-whitespace-and-blank-lines ()
  "Deletes all whitespace at the end of a buffer (or, rather, a
buffer's accessible portion, see `Narrowing'), including blank
lines."
  (interactive)
  (let ((point (point)))
    (delete-trailing-whitespace)
    (goto-char (point-max))
    (delete-blank-lines)
    (goto-char (min point (point-max)))))

;; make sure trailing whitespace is removed every time a buffer is saved.
(add-hook 'before-save-hook 'delete-trailing-whitespace-and-blank-lines)

;; globally install my indentation setup
(global-set-key "\r" 'my-enter)
(setq indent-line-function 'my-indent)

;; also override key setting of major-modes, if any
(add-hook 'after-change-major-mode-hook 'my-indentation-setup)

This works for me in Emacs 23, although I may have missed some edge cases. However, these changes are so fundamental that I predict you will run into incompatibilities sooner or later with some major-modes that expect indentation to work they set it up. If you really want to get into Emacs it's worthwhile adapting the habits you inherited from other editors to the way Emacs does things.

For soft word-wrap there is a minor-mode called "longlines" which you can download from here: http://www.emacswiki.org/cgi-bin/emacs/download/longlines.el I haven't used it so I can't tell you how well it works.

Thomas
  • 17,016
  • 4
  • 46
  • 70
  • Thanks. Regarding adapting the habits: I could do that, but the very way emacs indents things (in lua-mode) is not compatible with my coding guidelines. I should probably open a new question for that. – Alexander Gladysh Jan 10 '11 at 02:07
  • On why doesn't it work, see discussion here: http://stackoverflow.com/questions/4643206/how-to-configure-indentation-in-emacs-lua-mode – Alexander Gladysh Jan 10 '11 at 13:08
2

Fixing TAB and RETURN:

(global-set-key "\t" 'self-insert-command)
(global-set-key "\r" 'newline-and-indent)

Fill column (haven't tried): say ESC x customize-var, enter fill-column, set to 80.

Ulrich Schwarz
  • 7,598
  • 1
  • 36
  • 48
  • @Alexander: Put those two lines in `~/.emacs` (that is, the file named `.emacs` in your home directory), outside all parentheses to be safe. – ShreevatsaR Jan 09 '11 at 15:19
  • Note that global keybindings can be over-ridden by the major mode's local keymap, and both of those can be over-ridden by a minor mode's local keymap. So if any active mode binds those keys, the above will no longer function. You can define your own global minor mode in order to force your custom key bindings to take precedence over everything else. See: http://stackoverflow.com/questions/683425/globally-override-key-binding-in-emacs/1758639 – phils Jan 09 '11 at 20:21
  • @ShreevatsaR: Thanks. Since I use emacs starter kit, I guess, the proper place to store these is `~/.emacs.d/.el`. – Alexander Gladysh Jan 10 '11 at 02:05