Initial Commit
[packages] / xemacs-packages / text-modes / tabify.el
1 ;;; tabify.el --- tab conversion commands for XEmacs
2
3 ;; Copyright (C) 1985, 1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6
7 ;; This file is part of XEmacs.
8
9 ;; XEmacs is free software; you can redistribute it and/or modify it
10 ;; under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; XEmacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with XEmacs; see the file COPYING.  If not, write to the Free
21 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 ;; 02111-1307, USA.
23
24 ;;; Synched up with: FSF 19.34.
25
26 ;;; Commentary:
27
28 ;; Commands to optimize spaces to tabs or expand tabs to spaces in a region
29 ;; (`tabify' and `untabify').  The variable tab-width does the obvious.
30
31 ;;; Code:
32
33 ;;;###autoload
34 (defun untabify (start end)
35   "Convert all tabs in region to multiple spaces, preserving columns.
36 Called non-interactively, the region is specified by arguments
37 START and END, rather than by the position of point and mark.
38 The variable `tab-width' controls the spacing of tab stops."
39   (interactive "r")
40   (save-excursion
41     (save-restriction
42       (narrow-to-region (point-min) end)
43       (goto-char start)
44       (let ((percent 5))
45         (while (search-forward "\t" nil t)      ; faster than re-search
46           (let ((tab-beg (point))
47                 (column (current-column))
48                 (indent-tabs-mode nil))
49             (skip-chars-backward "\t" start)
50             (delete-region tab-beg (point))
51             ;; XEmacs change -- show progress
52             (indent-to column)
53             (if (> (/ (* 100 (- (point) start)) (- (point-max) start)) percent)
54                 (progn
55                   (message "untabify: %d%% ..." percent)
56                   (setq percent (+ 5 percent)))))))
57       (message "untabify: done")))
58   nil)
59
60 ;;;###autoload
61 (defun tabify (start end)
62   "Convert multiple spaces in region to tabs when possible.
63 A group of spaces is partially replaced by tabs
64 when this can be done without changing the column they end at.
65 Called non-interactively, the region is specified by arguments
66 START and END, rather than by the position of point and mark.
67 The variable `tab-width' controls the spacing of tab stops."
68   (interactive "r")
69   (save-excursion
70     (save-restriction
71       ;; Include the beginning of the line in the narrowing
72       ;; since otherwise it will throw off current-column.
73       (goto-char start)
74       (beginning-of-line)
75       (narrow-to-region (point) end)
76       (goto-char start)
77       (let ((percent 5))
78         (while (re-search-forward "[ \t][ \t][ \t]*" nil t)
79           (let ((column (current-column))
80                 (indent-tabs-mode t))
81             (delete-region (match-beginning 0) (point))
82             ;; XEmacs change -- show progress
83             (indent-to column)
84             (if (> (/ (* 100 (- (point) start)) (- (point-max) start)) percent)
85                 (progn
86                   (message "tabify: %d%% ..." percent)
87                   (setq percent (+ 5 percent)))))))
88       (message "tabify: done"))))
89
90 (provide 'tabify)
91
92 ;;; tabify.el ends here