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