]> code.delx.au - gnu-emacs/blob - lisp/tabify.el
*** empty log message ***
[gnu-emacs] / lisp / tabify.el
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 ;;;###autoload
22 (defun untabify (start end)
23 "Convert all tabs in region to multiple spaces, preserving columns.
24 The variable tab-width controls the action."
25 (interactive "r")
26 (save-excursion
27 (save-restriction
28 (narrow-to-region start end)
29 (goto-char start)
30 (while (search-forward "\t" nil t) ; faster than re-search
31 (let ((start (point))
32 (column (current-column))
33 (indent-tabs-mode nil))
34 (skip-chars-backward "\t")
35 (delete-region start (point))
36 (indent-to column))))))
37
38 ;;;###autoload
39 (defun tabify (start end)
40 "Convert multiple spaces in region to tabs when possible.
41 A group of spaces is partially replaced by tabs
42 when this can be done without changing the column they end at.
43 The variable tab-width controls the action."
44 (interactive "r")
45 (save-excursion
46 (save-restriction
47 (narrow-to-region start end)
48 (goto-char start)
49 (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
50 (let ((column (current-column))
51 (indent-tabs-mode t))
52 (delete-region (match-beginning 0) (point))
53 (indent-to column))))))