Commit | Line | Data |
---|---|---|
c88ab9ce ER |
1 | ;;; tabify.el --- tab conversion commands for Emacs |
2 | ||
0d30b337 | 3 | ;; Copyright (C) 1985, 1994, 2002, 2003, 2004, |
aaef169d | 4 | ;; 2005, 2006 Free Software Foundation, Inc. |
a2535589 | 5 | |
58142744 ER |
6 | ;; Maintainer: FSF |
7 | ||
a2535589 JA |
8 | ;; This file is part of GNU Emacs. |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
a2535589 JA |
13 | ;; any later version. |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
b578f267 | 21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
086add15 LK |
22 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 | ;; Boston, MA 02110-1301, USA. | |
a2535589 | 24 | |
d9ecc911 ER |
25 | ;;; Commentary: |
26 | ||
27 | ;; Commands to optimize spaces to tabs or expand tabs to spaces in a region | |
28 | ;; (`tabify' and `untabify'). The variable tab-width does the obvious. | |
29 | ||
22a89ee8 | 30 | ;;; Code: |
a2535589 | 31 | |
f9f9507e | 32 | ;;;###autoload |
a2535589 JA |
33 | (defun untabify (start end) |
34 | "Convert all tabs in region to multiple spaces, preserving columns. | |
e47b2570 RS |
35 | Called non-interactively, the region is specified by arguments |
36 | START and END, rather than by the position of point and mark. | |
37 | The variable `tab-width' controls the spacing of tab stops." | |
a2535589 JA |
38 | (interactive "r") |
39 | (save-excursion | |
40 | (save-restriction | |
5119b412 | 41 | (narrow-to-region (point-min) end) |
a2535589 JA |
42 | (goto-char start) |
43 | (while (search-forward "\t" nil t) ; faster than re-search | |
bb8143d9 | 44 | (forward-char -1) |
2d8e81a5 | 45 | (let ((tab-beg (point)) |
bb8143d9 KH |
46 | (indent-tabs-mode nil) |
47 | column) | |
48 | (skip-chars-forward "\t") | |
49 | (setq column (current-column)) | |
2d8e81a5 | 50 | (delete-region tab-beg (point)) |
a2535589 JA |
51 | (indent-to column)))))) |
52 | ||
bb8143d9 KH |
53 | (defvar tabify-regexp "[ \t][ \t]+" |
54 | "Regexp matching whitespace that tabify should consider. | |
55 | Usually this will be \"[ \\t][ \\t]+\" to match two or more spaces or tabs. | |
56 | \"^[ \\t]+\" is also useful, for tabifying only initial whitespace.") | |
57 | ||
f9f9507e | 58 | ;;;###autoload |
a2535589 JA |
59 | (defun tabify (start end) |
60 | "Convert multiple spaces in region to tabs when possible. | |
61 | A group of spaces is partially replaced by tabs | |
62 | when this can be done without changing the column they end at. | |
e47b2570 RS |
63 | Called non-interactively, the region is specified by arguments |
64 | START and END, rather than by the position of point and mark. | |
65 | The variable `tab-width' controls the spacing of tab stops." | |
a2535589 JA |
66 | (interactive "r") |
67 | (save-excursion | |
68 | (save-restriction | |
7be313ea RS |
69 | ;; Include the beginning of the line in the narrowing |
70 | ;; since otherwise it will throw off current-column. | |
71 | (goto-char start) | |
72 | (beginning-of-line) | |
73 | (narrow-to-region (point) end) | |
a2535589 | 74 | (goto-char start) |
bb8143d9 | 75 | (while (re-search-forward tabify-regexp nil t) |
a2535589 JA |
76 | (let ((column (current-column)) |
77 | (indent-tabs-mode t)) | |
78 | (delete-region (match-beginning 0) (point)) | |
79 | (indent-to column)))))) | |
c88ab9ce | 80 | |
85588571 RS |
81 | (provide 'tabify) |
82 | ||
ab5796a9 | 83 | ;;; arch-tag: c83893b1-e0cc-4e57-8a09-73fd03466416 |
c88ab9ce | 84 | ;;; tabify.el ends here |