Thursday, June 3, 2010

Emacs Spontaneously Shifting to DOS Mode. Yuck.

For some reason emacs (GNU Emacs 22.2.1 (i486-pc-linux-gnu, X toolkit, Xaw3d scroll bars) of 2010-03-26 on rothera, modified by Ubuntu) on Ubuntu 9.10 has been spontaneously deciding to use MS-DOS/Windows line endings rather than Unix. This is rather wasteful of space, and sometimes causes issues with other tools. It's also inconvenient, since I was killing the emacs buffer, running dos2unix on the file, and then loading the fixed file back into emacs. Looking for a fix, I stumbled across three blog entries of note:

1) http://www.debianadmin.com/flip-convert-text-file-line-endings-between-unix-and-dos-formats.html recommends a tool called flip. This makes no sense to me whatsoever since it does exactly what dos2unix and unix2dos do. It looks to me like someone's CS1 assignment that somehow got added to the Debian repositories.

2) http://www.sowbug.org/mt/2004/07/emacs-dos-line-endings.html has a good short-term fix, a bit more convenient than what I was doing. Within emacs, type the following key sequence: C-x [ENTER] f unix [ENTER]. This sets the "Coding system for saving file." Good enough.
Also, Sowbug.org has a great tagline: "If all you have is a Bloom filter, everything looks like a set whose membership you wish to test with a possibility of false positives."

3) Barriehie at http://ubuntuforums.org/showthread.php?t=1375454 has the winner, a good, permanent, catch-all type solution. Essentially, modify .emacs to include:

;; Set ALL files to UNIX line endings
(add-hook 'find-file-hook 'find-file-check-line-endings)
(defun dos-file-endings-p ()
(string-match "dos" (symbol-name buffer-file-coding-system)))
(defun find-file-check-line-endings ()
(when (dos-file-endings-p)
(set-buffer-file-coding-system 'iso-latin-1-unix t)
(set-buffer-modified-p nil)))

I've tested it just a bit, and it works beautifully.

Also, my .emacs is all in one file, and I prefer the way he splits it across multiple files. I'm using the .emacs that grad students were given at Ohio State ca. 1992 with many, many modifications. Following Barriehie's lead, I've split my .emacs it into multiple files, thus allowing me to keep my modifications separate from the stuff passed down from Brutus Buckeye.

No comments: