tabify.el 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. ;; Tab conversion commands for Emacs
  2. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. (defun untabify (start end)
  18. "Convert all tabs in region to multiple spaces, preserving columns.
  19. The variable tab-width controls the action."
  20. (interactive "r")
  21. (save-excursion
  22. (save-restriction
  23. (narrow-to-region start end)
  24. (goto-char start)
  25. (while (search-forward "\t" nil t) ; faster than re-search
  26. (let ((start (point))
  27. (column (current-column))
  28. (indent-tabs-mode nil))
  29. (skip-chars-backward "\t")
  30. (delete-region start (point))
  31. (indent-to column))))))
  32. (defun tabify (start end)
  33. "Convert multiple spaces in region to tabs when possible.
  34. A group of spaces is partially replaced by tabs
  35. when this can be done without changing the column they end at.
  36. The variable tab-width controls the action."
  37. (interactive "r")
  38. (save-excursion
  39. (save-restriction
  40. (narrow-to-region start end)
  41. (goto-char start)
  42. (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
  43. (let ((column (current-column))
  44. (indent-tabs-mode t))
  45. (delete-region (match-beginning 0) (point))
  46. (indent-to column))))))