Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'd like to untabify whenever I save a buffer. This worked fine until I upgraded to Emacs 24 which broke it, as there is no make-local-hook anymore. Have a look:

;;; untabify on C-x C-s (except for GNUmakefiles that needs tabs)
;;;   (note: `stringp` check to avoid bug (?) in HTML-mode)
(defun untab-all ()
  (unless (and (stringp mode-name)
               (string= mode-name "GNUmakefile") )
    (untabify (point-min) (point-max)) ))

;; TODO: doesn't work anymore as no make-local-hook in Emacs 24 - 
;; it won't work with simply dropping that line,
;; or doing that, and passing a 4th `t` argument in the second
;; add-hook call (which is the LOCAL parameter)

;; (add-hook 'emacs-lisp-mode-hook
;;    '(lambda ()
;;       (make-local-hook 'write-contents-hooks)
;;       (add-hook 'write-contents-hooks 'untab-all) ))

EDIT

Got it to work with the below help and this site.

Ironic thing is, with my old solution (using make-local-hook), somehow I got this globally (which is why I had to add that check for GNU makefiles).

Now, this seems to be a local thing: this is good as that check needn't be done at every save; only, you have to state for what modes untabify should be done. (I guess you could just add them one by one as you go along, every time you discover a tab, until you're done. Although it feels like you're using 5-6 modes, when you think about it, you use tons!)

But, to confuse things even more, the HTML mode seems to do this, with no intervention! If you read the code above, you see that even for the old solution, HTML was the cat among the ermines...

Anyway, this seems to work:

(defun untab-all ()
  (untabify (point-min) (point-max))
   nil ) ; did not write buffer to disk

(defun add-write-contents-hooks-hook ()
  (add-hook
   'write-contents-hooks
   'untab-all
     nil  ; APPEND  unrelated, explicit default nil as optional :)
     t )) ; LOCAL   non-nil => make hook local

;; more modes: http://www.emacswiki.org/CategoryModes
(add-hook 'emacs-lisp-mode-hook #'add-write-contents-hooks-hook)
(add-hook 'c-mode-common-hook   #'add-write-contents-hooks-hook)
(add-hook 'sh-mode-hook         #'add-write-contents-hooks-hook)
(add-hook 'text-mode-hook       #'add-write-contents-hooks-hook)
(add-hook 'sql-mode-hook        #'add-write-contents-hooks-hook)
(add-hook 'css-mode-hook        #'add-write-contents-hooks-hook)
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Emacs 24 uses the LOCAL parameter of add-hook instead of make-local-hook. (I believe that was added in Emacs 21.1, but make-local-hook wasn't removed until Emacs 24.)

Try this:

(add-hook 'emacs-lisp-mode-hook
    '(lambda ()
       (add-hook 'write-contents-hooks 'untab-all nil t) ))

Your other problem is that untab-all must return nil to indicate that it did not write the buffer to disk (as explained in the docs for write-contents-hooks, or as it's now called, write-contents-functions):

(defun untab-all ()
  (unless (and (stringp mode-name)
               (string= mode-name "GNUmakefile") )
    (untabify (point-min) (point-max)) )
  nil) ; did not write buffer to disk
share|improve this answer
    
That's the first thing I tried, and I say so above. It doesn't work: When I save, it gets stuck saving. If you can solve this, it'd be a lifesaver! –  Emanuel Berg Jan 19 '13 at 23:10
    
This is why you shouldn't just say "it doesn't work", but instead explain what happened and what you expected to happen. You were asking about make-local-hook, but your real problem had nothing to do with that. –  cjm Jan 19 '13 at 23:29
    
1. The title reads "untabify on save in Emacs 24 without make-local-hook" - is it not clear, what I seek? 2. I didn't "just say 'it doesn't work'" 3. While there might be an "other problem", this other problem is related as it worked fine until I upgraded Emacs. –  Emanuel Berg Jan 19 '13 at 23:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.