Mercurial > emacs
annotate lisp/rect.el @ 2318:50737ca2fd45
Decide automatically whether to use COFF or ELF.
| author | Richard M. Stallman <rms@gnu.org> |
|---|---|
| date | Mon, 22 Mar 1993 19:50:35 +0000 |
| parents | f287613dfc28 |
| children | e67f6d2679e3 |
| rev | line source |
|---|---|
|
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
1 ;;; rect.el --- rectangle functions for GNU Emacs. |
|
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
2 |
| 845 | 3 ;; Copyright (C) 1985 Free Software Foundation, Inc. |
| 4 | |
|
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
5 ;; Maintainer: FSF |
|
814
38b2499cb3e9
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
6 ;; Keywords: internal |
|
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
7 |
| 36 | 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 | |
|
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
789
diff
changeset
|
12 ;; the Free Software Foundation; either version 2, or (at your option) |
| 36 | 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 | |
|
2308
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1619
diff
changeset
|
24 ;;; Commentary: |
|
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1619
diff
changeset
|
25 |
|
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1619
diff
changeset
|
26 ;; This package provides the operations on rectangles that are ocumented |
|
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1619
diff
changeset
|
27 ;; in the Emacs manual. |
|
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1619
diff
changeset
|
28 |
|
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
29 ;;; Code: |
| 36 | 30 |
| 31 (defun operate-on-rectangle (function start end coerce-tabs) | |
| 32 "Call FUNCTION for each line of rectangle with corners at START, END. | |
| 33 If COERCE-TABS is non-nil, convert multi-column characters | |
| 34 that span the starting or ending columns on any line | |
| 35 to multiple spaces before calling FUNCTION. | |
| 36 FUNCTION 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. | |
| 40 Point 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) | |
|
1619
6147d2164331
* rect.el (operate-on-rectangle): Use move-to-column's FORCE
Jim Blandy <jimb@redhat.com>
parents:
1542
diff
changeset
|
53 (setq startcol (prog1 endcol (setq endcol startcol)))) |
| 36 | 54 (if (/= endcol startcol) |
| 55 (save-excursion | |
| 56 (goto-char startlinepos) | |
| 57 (while (< (point) endlinepos) | |
| 58 (let (startpos begextra endextra) | |
|
1619
6147d2164331
* rect.el (operate-on-rectangle): Use move-to-column's FORCE
Jim Blandy <jimb@redhat.com>
parents:
1542
diff
changeset
|
59 (move-to-column startcol coerce-tabs) |
| 36 | 60 (setq begextra (- (current-column) startcol)) |
| 61 (setq startpos (point)) | |
|
1619
6147d2164331
* rect.el (operate-on-rectangle): Use move-to-column's FORCE
Jim Blandy <jimb@redhat.com>
parents:
1542
diff
changeset
|
62 (move-to-column endcol coerce-tabs) |
| 36 | 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))) | |
| 70 | |
| 71 (defun delete-rectangle-line (startdelpos ignore ignore) | |
| 72 (delete-region startdelpos (point))) | |
| 73 | |
| 74 (defun delete-extract-rectangle-line (startdelpos begextra endextra) | |
| 75 (save-excursion | |
| 76 (extract-rectangle-line startdelpos begextra endextra)) | |
| 77 (delete-region startdelpos (point))) | |
| 78 | |
| 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)))) | |
| 95 | |
| 96 (defconst spaces-strings | |
| 97 '["" " " " " " " " " " " " " " " " "]) | |
| 98 | |
| 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))))) | |
| 106 | |
| 258 | 107 ;;;###autoload |
| 36 | 108 (defun delete-rectangle (start end) |
| 109 "Delete (don't save) text in rectangle with point and mark as corners. | |
| 242 | 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." | |
| 36 | 112 (interactive "r") |
| 113 (operate-on-rectangle 'delete-rectangle-line start end t)) | |
| 114 | |
| 258 | 115 ;;;###autoload |
| 36 | 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))) | |
| 124 | |
| 258 | 125 ;;;###autoload |
| 36 | 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))) | |
| 132 | |
| 133 (defvar killed-rectangle nil | |
| 134 "Rectangle for yank-rectangle to insert.") | |
| 135 | |
| 258 | 136 ;;;###autoload |
| 36 | 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. | |
| 242 | 140 But in programs you might prefer to use `delete-extract-rectangle'." |
| 36 | 141 (interactive "r") |
| 142 (setq killed-rectangle (delete-extract-rectangle start end))) | |
| 143 | |
| 258 | 144 ;;;###autoload |
| 36 | 145 (defun yank-rectangle () |
| 146 "Yank the last killed rectangle with upper left corner at point." | |
| 147 (interactive) | |
| 148 (insert-rectangle killed-rectangle)) | |
| 149 | |
| 258 | 150 ;;;###autoload |
| 36 | 151 (defun insert-rectangle (rectangle) |
| 152 "Insert text of RECTANGLE with upper left corner at point. | |
| 242 | 153 RECTANGLE's first line is inserted at point, its second |
| 154 line is inserted at a point vertically under point, etc. | |
|
1542
724b443e445d
(insert-rectangle): Put mark at upper left corner.
Richard M. Stallman <rms@gnu.org>
parents:
845
diff
changeset
|
155 RECTANGLE should be a list of strings. |
|
724b443e445d
(insert-rectangle): Put mark at upper left corner.
Richard M. Stallman <rms@gnu.org>
parents:
845
diff
changeset
|
156 After this command, the mark is at the upper left corner |
|
724b443e445d
(insert-rectangle): Put mark at upper left corner.
Richard M. Stallman <rms@gnu.org>
parents:
845
diff
changeset
|
157 and point is at the lower right corner." |
| 36 | 158 (let ((lines rectangle) |
| 159 (insertcolumn (current-column)) | |
| 160 (first t)) | |
|
1542
724b443e445d
(insert-rectangle): Put mark at upper left corner.
Richard M. Stallman <rms@gnu.org>
parents:
845
diff
changeset
|
161 (push-mark) |
| 36 | 162 (while lines |
| 163 (or first | |
| 164 (progn | |
| 165 (forward-line 1) | |
| 166 (or (bolp) (insert ?\n)) | |
|
1619
6147d2164331
* rect.el (operate-on-rectangle): Use move-to-column's FORCE
Jim Blandy <jimb@redhat.com>
parents:
1542
diff
changeset
|
167 (move-to-column insertcolumn t))) |
| 36 | 168 (setq first nil) |
| 169 (insert (car lines)) | |
| 170 (setq lines (cdr lines))))) | |
| 171 | |
| 258 | 172 ;;;###autoload |
| 36 | 173 (defun open-rectangle (start end) |
| 174 "Blank out rectangle with corners at point and mark, shifting text right. | |
| 175 The text previously in the region is not overwritten by the blanks, | |
|
1619
6147d2164331
* rect.el (operate-on-rectangle): Use move-to-column's FORCE
Jim Blandy <jimb@redhat.com>
parents:
1542
diff
changeset
|
176 but instead winds up to the right of the rectangle." |
| 36 | 177 (interactive "r") |
|
1619
6147d2164331
* rect.el (operate-on-rectangle): Use move-to-column's FORCE
Jim Blandy <jimb@redhat.com>
parents:
1542
diff
changeset
|
178 (operate-on-rectangle 'open-rectangle-line start end nil) |
|
6147d2164331
* rect.el (operate-on-rectangle): Use move-to-column's FORCE
Jim Blandy <jimb@redhat.com>
parents:
1542
diff
changeset
|
179 (goto-char start)) |
| 36 | 180 |
| 181 (defun open-rectangle-line (startpos begextra endextra) | |
| 182 (let ((column (+ (current-column) begextra endextra))) | |
| 183 (goto-char startpos) | |
| 184 (let ((ocol (current-column))) | |
| 185 (skip-chars-forward " \t") | |
| 186 (setq column (+ column (- (current-column) ocol)))) | |
| 187 (delete-region (point) | |
|
1619
6147d2164331
* rect.el (operate-on-rectangle): Use move-to-column's FORCE
Jim Blandy <jimb@redhat.com>
parents:
1542
diff
changeset
|
188 ;; Use skip-chars-backward's LIM argument to leave |
|
6147d2164331
* rect.el (operate-on-rectangle): Use move-to-column's FORCE
Jim Blandy <jimb@redhat.com>
parents:
1542
diff
changeset
|
189 ;; characters before STARTPOS undisturbed. |
|
6147d2164331
* rect.el (operate-on-rectangle): Use move-to-column's FORCE
Jim Blandy <jimb@redhat.com>
parents:
1542
diff
changeset
|
190 (progn (skip-chars-backward " \t" startpos) |
| 36 | 191 (point))) |
| 192 (indent-to column))) | |
| 193 | |
| 258 | 194 ;;;###autoload |
| 36 | 195 (defun clear-rectangle (start end) |
| 196 "Blank out rectangle with corners at point and mark. | |
| 197 The text previously in the region is overwritten by the blanks. | |
| 198 When called from a program, requires two args which specify the corners." | |
| 199 (interactive "r") | |
| 200 (operate-on-rectangle 'clear-rectangle-line start end t)) | |
| 201 | |
| 202 (defun clear-rectangle-line (startpos begextra endextra) | |
| 203 (skip-chars-forward " \t") | |
| 204 (let ((column (+ (current-column) endextra))) | |
| 205 (delete-region (point) | |
| 206 (progn (goto-char startpos) | |
| 207 (skip-chars-backward " \t") | |
| 208 (point))) | |
| 209 (indent-to column))) | |
| 210 | |
|
1619
6147d2164331
* rect.el (operate-on-rectangle): Use move-to-column's FORCE
Jim Blandy <jimb@redhat.com>
parents:
1542
diff
changeset
|
211 (provide 'rect) |
|
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
212 |
|
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
213 ;;; rect.el ends here |
