Initial revision
[bpt/emacs.git] / lisp / tabify.el
CommitLineData
a2535589
JA
1;; Tab conversion commands for Emacs
2;; Copyright (C) 1985 Free Software Foundation, Inc.
3
4;; This file is part of GNU Emacs.
5
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 1, or (at your option)
9;; any later version.
10
11;; GNU Emacs is distributed in the hope that it will be useful,
12;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;; GNU General Public License for more details.
15
16;; You should have received a copy of the GNU General Public License
17;; along with GNU Emacs; see the file COPYING. If not, write to
18;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19
20
21(defun untabify (start end)
22 "Convert all tabs in region to multiple spaces, preserving columns.
23The variable tab-width controls the action."
24 (interactive "r")
25 (save-excursion
26 (save-restriction
27 (narrow-to-region start end)
28 (goto-char start)
29 (while (search-forward "\t" nil t) ; faster than re-search
30 (let ((start (point))
31 (column (current-column))
32 (indent-tabs-mode nil))
33 (skip-chars-backward "\t")
34 (delete-region start (point))
35 (indent-to column))))))
36
37(defun tabify (start end)
38 "Convert multiple spaces in region to tabs when possible.
39A group of spaces is partially replaced by tabs
40when this can be done without changing the column they end at.
41The variable tab-width controls the action."
42 (interactive "r")
43 (save-excursion
44 (save-restriction
45 (narrow-to-region start end)
46 (goto-char start)
47 (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
48 (let ((column (current-column))
49 (indent-tabs-mode t))
50 (delete-region (match-beginning 0) (point))
51 (indent-to column))))))