tabify.el 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ;;; tabify.el --- tab conversion commands for Emacs
  2. ;; Copyright (C) 1985, 1994, 2001-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Package: emacs
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Commands to optimize spaces to tabs or expand tabs to spaces in a region
  18. ;; (`tabify' and `untabify'). The variable tab-width does the obvious.
  19. ;;; Code:
  20. ;;;###autoload
  21. (defun untabify (start end)
  22. "Convert all tabs in region to multiple spaces, preserving columns.
  23. Called non-interactively, the region is specified by arguments
  24. START and END, rather than by the position of point and mark.
  25. The variable `tab-width' controls the spacing of tab stops."
  26. (interactive "r")
  27. (let ((c (current-column)))
  28. (save-excursion
  29. (save-restriction
  30. (narrow-to-region (point-min) end)
  31. (goto-char start)
  32. (while (search-forward "\t" nil t) ; faster than re-search
  33. (forward-char -1)
  34. (let ((tab-beg (point))
  35. (indent-tabs-mode nil)
  36. column)
  37. (skip-chars-forward "\t")
  38. (setq column (current-column))
  39. (delete-region tab-beg (point))
  40. (indent-to column)))))
  41. (move-to-column c)))
  42. (defvar tabify-regexp " [ \t]+"
  43. "Regexp matching whitespace that tabify should consider.
  44. Usually this will be \" [ \\t]+\" to match a space followed by whitespace.
  45. \"^\\t* [ \\t]+\" is also useful, for tabifying only initial whitespace.")
  46. ;;;###autoload
  47. (defun tabify (start end)
  48. "Convert multiple spaces in region to tabs when possible.
  49. A group of spaces is partially replaced by tabs
  50. when this can be done without changing the column they end at.
  51. Called non-interactively, the region is specified by arguments
  52. START and END, rather than by the position of point and mark.
  53. The variable `tab-width' controls the spacing of tab stops."
  54. (interactive "r")
  55. (save-excursion
  56. (save-restriction
  57. ;; Include the beginning of the line in the narrowing
  58. ;; since otherwise it will throw off current-column.
  59. (goto-char start)
  60. (beginning-of-line)
  61. (narrow-to-region (point) end)
  62. (goto-char start)
  63. (let ((indent-tabs-mode t))
  64. (while (re-search-forward tabify-regexp nil t)
  65. ;; The region between (match-beginning 0) and (match-end 0) is just
  66. ;; spacing which we want to adjust to use TABs where possible.
  67. (let ((end-col (current-column))
  68. (beg-col (save-excursion (goto-char (match-beginning 0))
  69. (skip-chars-forward "\t")
  70. (current-column))))
  71. (if (= (/ end-col tab-width) (/ beg-col tab-width))
  72. ;; The spacing (after some leading TABs which we wouldn't
  73. ;; want to touch anyway) does not straddle a TAB boundary,
  74. ;; so it neither contains a TAB, nor will we be able to use
  75. ;; a TAB here anyway: there's nothing to do.
  76. nil
  77. (delete-region (match-beginning 0) (point))
  78. (indent-to end-col))))))))
  79. (provide 'tabify)
  80. ;;; tabify.el ends here