Search code examples
elisp

Converting this untabify on-save hook for emacs to work with espresso-mode (or all modes)


I found this snippet to add to my .emacs that will, on save, strip out tabs and replace them with spaces (to help my files play nicely with everyone else on the team who uses spaces).

Unfortunately, my lisp and emacs lisp chops are not very strong. It seems to be this snippet will work only for the java major mode - how can I get this to work with espresso-mode?

(defun java-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 'java-mode-hook 
            (lambda ()
               (add-hook 'write-contents-hooks 'java-mode-untabify nil 'local)))

Solution

  • I have following code in my .emacs that works for me:

    ;; untabify some modes
    (setq alexott/untabify-modes '(haskell-mode emacs-lisp-mode lisp-mode scheme-mode
                                            erlang-mode clojure-mode))
    (defun alexott/untabify-hook ()
      (when (member major-mode alexott/untabify-modes)
         (untabify (point-min) (point-max))))
    (add-hook 'before-save-hook 'alexott/untabify-hook)
    

    you can add espresso-mode into list of modes, for which untabify will perfomed, by changing list in alexott/untabify-modes variable following way:

    (setq alexott/untabify-modes '(haskell-mode emacs-lisp-mode lisp-mode scheme-mode
                                            erlang-mode clojure-mode espresso-mode))