Check for krb and des libraries.
[emacs.git] / lisp / rect.el
blobd8e742ce6a1b078f110ab3c56a1ca2fc3ed3c475
1 ;;; rect.el --- rectangle functions for GNU Emacs.
3 ;; Copyright (C) 1985, 1994 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: internal
8 ;; This file is part of GNU Emacs.
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This package provides the operations on rectangles that are ocumented
28 ;; in the Emacs manual.
30 ;;; Code:
32 (defun operate-on-rectangle (function start end coerce-tabs)
33 "Call FUNCTION for each line of rectangle with corners at START, END.
34 If COERCE-TABS is non-nil, convert multi-column characters
35 that span the starting or ending columns on any line
36 to multiple spaces before calling FUNCTION.
37 FUNCTION is called with three arguments:
38 position of start of segment of this line within the rectangle,
39 number of columns that belong to rectangle but are before that position,
40 number of columns that belong to rectangle but are after point.
41 Point is at the end of the segment of this line within the rectangle."
42 (let (startcol startlinepos endcol endlinepos)
43 (save-excursion
44 (goto-char start)
45 (setq startcol (current-column))
46 (beginning-of-line)
47 (setq startlinepos (point)))
48 (save-excursion
49 (goto-char end)
50 (setq endcol (current-column))
51 (forward-line 1)
52 (setq endlinepos (point-marker)))
53 (if (< endcol startcol)
54 (setq startcol (prog1 endcol (setq endcol startcol))))
55 (save-excursion
56 (goto-char startlinepos)
57 (while (< (point) endlinepos)
58 (let (startpos begextra endextra)
59 (move-to-column startcol coerce-tabs)
60 (setq begextra (- (current-column) startcol))
61 (setq startpos (point))
62 (move-to-column endcol coerce-tabs)
63 (setq endextra (- endcol (current-column)))
64 (if (< begextra 0)
65 (setq endextra (+ endextra begextra)
66 begextra 0))
67 (funcall function startpos begextra endextra))
68 (forward-line 1)))
69 (- endcol startcol)))
71 (defun delete-rectangle-line (startdelpos ignore ignore)
72 (delete-region startdelpos (point)))
74 (defun delete-extract-rectangle-line (startdelpos begextra endextra)
75 (save-excursion
76 (extract-rectangle-line startdelpos begextra endextra))
77 (delete-region startdelpos (point)))
79 (defun extract-rectangle-line (startdelpos begextra endextra)
80 (let ((line (buffer-substring startdelpos (point)))
81 (end (point)))
82 (goto-char startdelpos)
83 (while (search-forward "\t" end t)
84 (let ((width (- (current-column)
85 (save-excursion (forward-char -1)
86 (current-column)))))
87 (setq line (concat (substring line 0 (- (point) end 1))
88 (spaces-string width)
89 (substring line (+ (length line) (- (point) end)))))))
90 (if (or (> begextra 0) (> endextra 0))
91 (setq line (concat (spaces-string begextra)
92 line
93 (spaces-string endextra))))
94 (setq lines (cons line lines))))
96 (defconst spaces-strings
97 '["" " " " " " " " " " " " " " " " "])
99 (defun spaces-string (n)
100 (if (<= n 8) (aref spaces-strings n)
101 (let ((val ""))
102 (while (> n 8)
103 (setq val (concat " " val)
104 n (- n 8)))
105 (concat val (aref spaces-strings n)))))
107 ;;;###autoload
108 (defun delete-rectangle (start end)
109 "Delete (don't save) text in rectangle with point and mark as corners.
110 The same range of columns is deleted in each line starting with the line
111 where the region begins and ending with the line where the region ends."
112 (interactive "r")
113 (operate-on-rectangle 'delete-rectangle-line start end t))
115 ;;;###autoload
116 (defun delete-extract-rectangle (start end)
117 "Delete contents of rectangle and return it as a list of strings.
118 Arguments START and END are the corners of the rectangle.
119 The value is list of strings, one for each line of the rectangle."
120 (let (lines)
121 (operate-on-rectangle 'delete-extract-rectangle-line
122 start end t)
123 (nreverse lines)))
125 ;;;###autoload
126 (defun extract-rectangle (start end)
127 "Return contents of rectangle with corners at START and END.
128 Value is list of strings, one for each line of the rectangle."
129 (let (lines)
130 (operate-on-rectangle 'extract-rectangle-line start end nil)
131 (nreverse lines)))
133 (defvar killed-rectangle nil
134 "Rectangle for yank-rectangle to insert.")
136 ;;;###autoload
137 (defun kill-rectangle (start end)
138 "Delete rectangle with corners at point and mark; save as last killed one.
139 Calling from program, supply two args START and END, buffer positions.
140 But in programs you might prefer to use `delete-extract-rectangle'."
141 (interactive "r")
142 (if buffer-read-only
143 (progn
144 (setq killed-rectangle (extract-rectangle start end))
145 (barf-if-buffer-read-only)))
146 (setq killed-rectangle (delete-extract-rectangle start end)))
148 ;;;###autoload
149 (defun yank-rectangle ()
150 "Yank the last killed rectangle with upper left corner at point."
151 (interactive)
152 (insert-rectangle killed-rectangle))
154 ;;;###autoload
155 (defun insert-rectangle (rectangle)
156 "Insert text of RECTANGLE with upper left corner at point.
157 RECTANGLE's first line is inserted at point, its second
158 line is inserted at a point vertically under point, etc.
159 RECTANGLE should be a list of strings.
160 After this command, the mark is at the upper left corner
161 and point is at the lower right corner."
162 (let ((lines rectangle)
163 (insertcolumn (current-column))
164 (first t))
165 (push-mark)
166 (while lines
167 (or first
168 (progn
169 (forward-line 1)
170 (or (bolp) (insert ?\n))
171 (move-to-column insertcolumn t)))
172 (setq first nil)
173 (insert (car lines))
174 (setq lines (cdr lines)))))
176 ;;;###autoload
177 (defun open-rectangle (start end)
178 "Blank out rectangle with corners at point and mark, shifting text right.
179 The text previously in the region is not overwritten by the blanks,
180 but instead winds up to the right of the rectangle."
181 (interactive "r")
182 (operate-on-rectangle 'open-rectangle-line start end nil)
183 (goto-char start))
185 (defun open-rectangle-line (startpos begextra endextra)
186 ;; Column where rectangle ends.
187 (let ((endcol (+ (current-column) endextra))
188 whitewidth)
189 (goto-char startpos)
190 ;; Column where rectangle begins.
191 (let ((begcol (- (current-column) begextra)))
192 (skip-chars-forward " \t")
193 ;; Width of whitespace to be deleted and recreated.
194 (setq whitewidth (- (current-column) begcol)))
195 ;; Delete the whitespace following the start column.
196 (delete-region startpos (point))
197 ;; Open the desired width, plus same amount of whitespace we just deleted.
198 (indent-to (+ endcol whitewidth))))
200 ;;;###autoload
201 (defun string-rectangle (start end string)
202 "Insert STRING on each line of the region-rectangle, shifting text right.
203 The left edge of the rectangle specifies the column for insertion.
204 This command does not delete or overwrite any existing text.
206 Called from a program, takes three args; START, END and STRING."
207 (interactive "r\nsString rectangle: ")
208 (operate-on-rectangle 'string-rectangle-line start end t))
210 (defun string-rectangle-line (startpos begextra endextra)
211 (let (whitespace)
212 (goto-char startpos)
213 ;; Compute horizontal width of following whitespace.
214 (let ((ocol (current-column)))
215 (skip-chars-forward " \t")
216 (setq whitespace (- (current-column) ocol)))
217 ;; Delete the following whitespace.
218 (delete-region startpos (point))
219 ;; Insert the desired string.
220 (insert string)
221 ;; Insert the same width of whitespace that we had before.
222 (indent-to (+ (current-column) whitespace))))
224 ;;;###autoload
225 (defun clear-rectangle (start end)
226 "Blank out rectangle with corners at point and mark.
227 The text previously in the region is overwritten by the blanks.
228 When called from a program, requires two args which specify the corners."
229 (interactive "r")
230 (operate-on-rectangle 'clear-rectangle-line start end t))
232 (defun clear-rectangle-line (startpos begextra endextra)
233 ;; Find end of whitespace after the rectangle.
234 (skip-chars-forward " \t")
235 (let ((column (+ (current-column) endextra)))
236 ;; Delete the text in the rectangle, and following whitespace.
237 (delete-region (point)
238 (progn (goto-char startpos)
239 (skip-chars-backward " \t")
240 (point)))
241 ;; Reindent out to same column that we were at.
242 (indent-to column)))
244 (provide 'rect)
246 ;;; rect.el ends here