Commit | Line | Data |
---|---|---|
c88ab9ce ER |
1 | ;;; tabify.el --- tab conversion commands for Emacs |
2 | ||
acaf905b | 3 | ;; Copyright (C) 1985, 1994, 2001-2012 Free Software Foundation, Inc. |
a2535589 | 4 | |
58142744 | 5 | ;; Maintainer: FSF |
bd78fa1d | 6 | ;; Package: emacs |
58142744 | 7 | |
a2535589 JA |
8 | ;; This file is part of GNU Emacs. |
9 | ||
eb3fa2cf | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
a2535589 | 11 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
a2535589 JA |
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 | |
eb3fa2cf | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
a2535589 | 22 | |
d9ecc911 ER |
23 | ;;; Commentary: |
24 | ||
25 | ;; Commands to optimize spaces to tabs or expand tabs to spaces in a region | |
26 | ;; (`tabify' and `untabify'). The variable tab-width does the obvious. | |
27 | ||
22a89ee8 | 28 | ;;; Code: |
a2535589 | 29 | |
f9f9507e | 30 | ;;;###autoload |
a2535589 JA |
31 | (defun untabify (start end) |
32 | "Convert all tabs in region to multiple spaces, preserving columns. | |
e47b2570 RS |
33 | Called non-interactively, the region is specified by arguments |
34 | START and END, rather than by the position of point and mark. | |
35 | The variable `tab-width' controls the spacing of tab stops." | |
a2535589 | 36 | (interactive "r") |
bead9a43 JB |
37 | (let ((c (current-column))) |
38 | (save-excursion | |
39 | (save-restriction | |
40 | (narrow-to-region (point-min) end) | |
41 | (goto-char start) | |
42 | (while (search-forward "\t" nil t) ; faster than re-search | |
43 | (forward-char -1) | |
44 | (let ((tab-beg (point)) | |
45 | (indent-tabs-mode nil) | |
46 | column) | |
47 | (skip-chars-forward "\t") | |
48 | (setq column (current-column)) | |
49 | (delete-region tab-beg (point)) | |
50 | (indent-to column))))) | |
51 | (move-to-column c))) | |
a2535589 | 52 | |
49c7d1ac | 53 | (defvar tabify-regexp " [ \t]+" |
bb8143d9 | 54 | "Regexp matching whitespace that tabify should consider. |
e5b1b45c | 55 | Usually this will be \" [ \\t]+\" to match a space followed by whitespace. |
49c7d1ac | 56 | \"^\\t* [ \\t]+\" is also useful, for tabifying only initial whitespace.") |
bb8143d9 | 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) |
49c7d1ac SM |
75 | (let ((indent-tabs-mode t)) |
76 | (while (re-search-forward tabify-regexp nil t) | |
77 | ;; The region between (match-beginning 0) and (match-end 0) is just | |
78 | ;; spacing which we want to adjust to use TABs where possible. | |
79 | (let ((end-col (current-column)) | |
80 | (beg-col (save-excursion (goto-char (match-beginning 0)) | |
81 | (skip-chars-forward "\t") | |
82 | (current-column)))) | |
83 | (if (= (/ end-col tab-width) (/ beg-col tab-width)) | |
84 | ;; The spacing (after some leading TABs which we wouldn't | |
85 | ;; want to touch anyway) does not straddle a TAB boundary, | |
86 | ;; so it neither contains a TAB, nor will we be able to use | |
87 | ;; a TAB here anyway: there's nothing to do. | |
88 | nil | |
89 | (delete-region (match-beginning 0) (point)) | |
90 | (indent-to end-col)))))))) | |
c88ab9ce | 91 | |
85588571 RS |
92 | (provide 'tabify) |
93 | ||
c88ab9ce | 94 | ;;; tabify.el ends here |