annotate lisp/prim/tabify.el @ 0:376386a54a3c r19-14

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