I have the following in my .emacs file:
(defun c++-mode-untabify ()
(save-excursion
(goto-char (point-min))
(while (re-search-forward "[ \t]+$" nil t)
(delete-region (match-beginning 0) (match-end 0)))
(goto-char (point-min))
(if (search-forward "\t" nil t)
(untabify (1- (point)) (point-max))))
nil)
(add-hook 'c++-mode-hook
'(lambda ()
(make-local-hook 'write-contents-hooks)
(add-hook 'write-contents-hooks 'c++-mode-untabify)))
Mostly ripped off from http://www.jwz.org/doc/tabs-vs-spaces.html. This causes emacs to run untabify
on the buffer before saving a C++ file.
The problem is that after I have loaded a C++ file, the untabify
hook is being applied to all subsequent file writes, even for buffers of other file types. This means that if I open a C++ file and then edit, say, a tab-delimited text file, the tabs get clobbered when saving the file.
I'm not an elisp guru, but I think the (make-local-hook 'write-contents-hooks)
line is trying to make the addition to write-contents-hooks
apply only to the local buffer. However, it isn't working, and c++-mode-untabify
is in write-contents-hooks
for all buffers.
I'm using EmacsW32 22.0 on a Windows XP box. Does anyone have any idea how to make the write-contents-hooks
change local to a specific buffer or how to reset it to nil
when switching to other, non-C++ buffers?