| 1 | ;;; rect.el --- rectangle functions for GNU Emacs |
| 2 | |
| 3 | ;; Copyright (C) 1985, 1999, 2000, 2001 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Maintainer: Didier Verna <didier@xemacs.org> |
| 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 the |
| 22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | ;; Boston, MA 02111-1307, USA. |
| 24 | |
| 25 | ;;; Commentary: |
| 26 | |
| 27 | ;; This package provides the operations on rectangles that are documented |
| 28 | ;; in the Emacs manual. |
| 29 | |
| 30 | ;; ### NOTE: this file has been almost completely rewritten by Didier Verna |
| 31 | ;; <didier@xemacs.org> in July 1999. The purpose of this rewrite is to be less |
| 32 | ;; intrusive and fill lines with whitespaces only when needed. A few functions |
| 33 | ;; are untouched though, as noted above their definition. |
| 34 | |
| 35 | |
| 36 | ;;; Code: |
| 37 | |
| 38 | ;;;###autoload |
| 39 | (defun move-to-column-force (column &optional flag) |
| 40 | "Move point to column COLUMN rigidly in the current line. |
| 41 | If COLUMN is within a multi-column character, replace it by |
| 42 | spaces and tab. |
| 43 | |
| 44 | As for `move-to-column', passing anything but nil or t in FLAG will move to |
| 45 | the desired column only if the line is long enough." |
| 46 | (let ((col (move-to-column column (or flag t)))) |
| 47 | (if (> col column) |
| 48 | (let (pos) |
| 49 | (delete-char -1) |
| 50 | (insert-char ? (- column (current-column))) |
| 51 | (setq pos (point)) |
| 52 | (indent-to col) |
| 53 | (goto-char pos))) |
| 54 | column)) |
| 55 | |
| 56 | ;; not used any more --dv |
| 57 | ;; extract-rectangle-line stores lines into this list |
| 58 | ;; to accumulate them for extract-rectangle and delete-extract-rectangle. |
| 59 | (defvar operate-on-rectangle-lines) |
| 60 | |
| 61 | ;; ### NOTE: this function is untouched, but not used anymore apart from |
| 62 | ;; `delete-whitespace-rectangle'. `apply-on-rectangle' is used instead. --dv |
| 63 | (defun operate-on-rectangle (function start end coerce-tabs) |
| 64 | "Call FUNCTION for each line of rectangle with corners at START, END. |
| 65 | If COERCE-TABS is non-nil, convert multi-column characters |
| 66 | that span the starting or ending columns on any line |
| 67 | to multiple spaces before calling FUNCTION. |
| 68 | FUNCTION is called with three arguments: |
| 69 | position of start of segment of this line within the rectangle, |
| 70 | number of columns that belong to rectangle but are before that position, |
| 71 | number of columns that belong to rectangle but are after point. |
| 72 | Point is at the end of the segment of this line within the rectangle." |
| 73 | (let (startcol startlinepos endcol endlinepos) |
| 74 | (save-excursion |
| 75 | (goto-char start) |
| 76 | (setq startcol (current-column)) |
| 77 | (beginning-of-line) |
| 78 | (setq startlinepos (point))) |
| 79 | (save-excursion |
| 80 | (goto-char end) |
| 81 | (setq endcol (current-column)) |
| 82 | (forward-line 1) |
| 83 | (setq endlinepos (point-marker))) |
| 84 | (if (< endcol startcol) |
| 85 | (setq startcol (prog1 endcol (setq endcol startcol)))) |
| 86 | (save-excursion |
| 87 | (goto-char startlinepos) |
| 88 | (while (< (point) endlinepos) |
| 89 | (let (startpos begextra endextra) |
| 90 | (if coerce-tabs |
| 91 | (move-to-column-force startcol) |
| 92 | (move-to-column startcol)) |
| 93 | (setq begextra (- (current-column) startcol)) |
| 94 | (setq startpos (point)) |
| 95 | (if coerce-tabs |
| 96 | (move-to-column-force endcol) |
| 97 | (move-to-column endcol)) |
| 98 | ;; If we overshot, move back one character |
| 99 | ;; so that endextra will be positive. |
| 100 | (if (and (not coerce-tabs) (> (current-column) endcol)) |
| 101 | (backward-char 1)) |
| 102 | (setq endextra (- endcol (current-column))) |
| 103 | (if (< begextra 0) |
| 104 | (setq endextra (+ endextra begextra) |
| 105 | begextra 0)) |
| 106 | (funcall function startpos begextra endextra)) |
| 107 | (forward-line 1))) |
| 108 | (- endcol startcol))) |
| 109 | |
| 110 | ;; The replacement for `operate-on-rectangle' -- dv |
| 111 | (defun apply-on-rectangle (function start end &rest args) |
| 112 | "Call FUNCTION for each line of rectangle with corners at START, END. |
| 113 | FUNCTION is called with two arguments: the start and end columns of the |
| 114 | rectangle, plus ARGS extra arguments. Point is at the beginning of line when |
| 115 | the function is called." |
| 116 | (let (startcol startpt endcol endpt) |
| 117 | (save-excursion |
| 118 | (goto-char start) |
| 119 | (setq startcol (current-column)) |
| 120 | (beginning-of-line) |
| 121 | (setq startpt (point)) |
| 122 | (goto-char end) |
| 123 | (setq endcol (current-column)) |
| 124 | (forward-line 1) |
| 125 | (setq endpt (point-marker)) |
| 126 | ;; ensure the start column is the left one. |
| 127 | (if (< endcol startcol) |
| 128 | (let ((col startcol)) |
| 129 | (setq startcol endcol endcol col))) |
| 130 | ;; start looping over lines |
| 131 | (goto-char startpt) |
| 132 | (while (< (point) endpt) |
| 133 | (apply function startcol endcol args) |
| 134 | (forward-line 1))) |
| 135 | )) |
| 136 | |
| 137 | (defun delete-rectangle-line (startcol endcol fill) |
| 138 | (let ((pt (line-end-position))) |
| 139 | (when (= (move-to-column-force startcol (or fill 'coerce)) startcol) |
| 140 | (if (and (not fill) (<= pt endcol)) |
| 141 | (delete-region (point) pt) |
| 142 | ;; else |
| 143 | (setq pt (point)) |
| 144 | (move-to-column-force endcol) |
| 145 | (delete-region pt (point)))) |
| 146 | )) |
| 147 | |
| 148 | (defun delete-extract-rectangle-line (startcol endcol lines fill) |
| 149 | (let ((pt (point-at-eol))) |
| 150 | (if (< (move-to-column-force startcol (or fill 'coerce)) startcol) |
| 151 | (setcdr lines (cons (spaces-string (- endcol startcol)) |
| 152 | (cdr lines))) |
| 153 | ;; else |
| 154 | (setq pt (point)) |
| 155 | (move-to-column-force endcol) |
| 156 | (setcdr lines (cons (buffer-substring pt (point)) (cdr lines))) |
| 157 | (delete-region pt (point))) |
| 158 | )) |
| 159 | |
| 160 | ;; ### NOTE: this is actually the only function that needs to do complicated |
| 161 | ;; stuff like what's happening in `operate-on-rectangle', because the buffer |
| 162 | ;; might be read-only. --dv |
| 163 | (defun extract-rectangle-line (startcol endcol lines) |
| 164 | (let (start end begextra endextra line) |
| 165 | (move-to-column startcol) |
| 166 | (setq start (point) |
| 167 | begextra (- (current-column) startcol)) |
| 168 | (move-to-column endcol) |
| 169 | (setq end (point) |
| 170 | endextra (- endcol (current-column))) |
| 171 | (setq line (buffer-substring start (point))) |
| 172 | (if (< begextra 0) |
| 173 | (setq endextra (+ endextra begextra) |
| 174 | begextra 0)) |
| 175 | (if (< endextra 0) |
| 176 | (setq endextra 0)) |
| 177 | (goto-char start) |
| 178 | (while (search-forward "\t" end t) |
| 179 | (let ((width (- (current-column) |
| 180 | (save-excursion (forward-char -1) |
| 181 | (current-column))))) |
| 182 | (setq line (concat (substring line 0 (- (point) end 1)) |
| 183 | (spaces-string width) |
| 184 | (substring line (+ (length line) |
| 185 | (- (point) end))))))) |
| 186 | (if (or (> begextra 0) (> endextra 0)) |
| 187 | (setq line (concat (spaces-string begextra) |
| 188 | line |
| 189 | (spaces-string endextra)))) |
| 190 | (setcdr lines (cons line (cdr lines))))) |
| 191 | |
| 192 | (defconst spaces-strings |
| 193 | '["" " " " " " " " " " " " " " " " "]) |
| 194 | |
| 195 | ;; this one is untouched --dv |
| 196 | (defun spaces-string (n) |
| 197 | (if (<= n 8) (aref spaces-strings n) |
| 198 | (let ((val "")) |
| 199 | (while (> n 8) |
| 200 | (setq val (concat " " val) |
| 201 | n (- n 8))) |
| 202 | (concat val (aref spaces-strings n))))) |
| 203 | |
| 204 | ;;;###autoload |
| 205 | (defun delete-rectangle (start end &optional fill) |
| 206 | "Delete (don't save) text in the region-rectangle. |
| 207 | The same range of columns is deleted in each line starting with the |
| 208 | line where the region begins and ending with the line where the region |
| 209 | ends. |
| 210 | |
| 211 | When called from a program the rectangle's corners are START and END. |
| 212 | With a prefix (or a FILL) argument, also fill lines where nothing has |
| 213 | to be deleted." |
| 214 | (interactive "*r\nP") |
| 215 | (apply-on-rectangle 'delete-rectangle-line start end fill)) |
| 216 | |
| 217 | ;;;###autoload |
| 218 | (defun delete-extract-rectangle (start end &optional fill) |
| 219 | "Delete the contents of the rectangle with corners at START and END. |
| 220 | Return it as a list of strings, one for each line of the rectangle. |
| 221 | |
| 222 | When called from a program the rectangle's corners are START and END. |
| 223 | With an optional FILL argument, also fill lines where nothing has to be |
| 224 | deleted." |
| 225 | (let ((lines (list nil))) |
| 226 | (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill) |
| 227 | (nreverse (cdr lines)))) |
| 228 | |
| 229 | ;;;###autoload |
| 230 | (defun extract-rectangle (start end) |
| 231 | "Return the contents of the rectangle with corners at START and END. |
| 232 | Return it as a list of strings, one for each line of the rectangle." |
| 233 | (let ((lines (list nil))) |
| 234 | (apply-on-rectangle 'extract-rectangle-line start end lines) |
| 235 | (nreverse (cdr lines)))) |
| 236 | |
| 237 | (defvar killed-rectangle nil |
| 238 | "Rectangle for `yank-rectangle' to insert.") |
| 239 | |
| 240 | ;;;###autoload |
| 241 | (defun kill-rectangle (start end &optional fill) |
| 242 | "Delete the region-rectangle and save it as the last killed one. |
| 243 | |
| 244 | When called from a program the rectangle's corners are START and END. |
| 245 | You might prefer to use `delete-extract-rectangle' from a program. |
| 246 | |
| 247 | With a prefix (or a FILL) argument, also fill lines where nothing has to be |
| 248 | deleted." |
| 249 | (interactive "*r\nP") |
| 250 | (when buffer-read-only |
| 251 | (setq killed-rectangle (extract-rectangle start end)) |
| 252 | (barf-if-buffer-read-only)) |
| 253 | (setq killed-rectangle (delete-extract-rectangle start end fill))) |
| 254 | |
| 255 | ;; this one is untouched --dv |
| 256 | ;;;###autoload |
| 257 | (defun yank-rectangle () |
| 258 | "Yank the last killed rectangle with upper left corner at point." |
| 259 | (interactive "*") |
| 260 | (insert-rectangle killed-rectangle)) |
| 261 | |
| 262 | ;; this one is untoutched --dv |
| 263 | ;;;###autoload |
| 264 | (defun insert-rectangle (rectangle) |
| 265 | "Insert text of RECTANGLE with upper left corner at point. |
| 266 | RECTANGLE's first line is inserted at point, its second |
| 267 | line is inserted at a point vertically under point, etc. |
| 268 | RECTANGLE should be a list of strings. |
| 269 | After this command, the mark is at the upper left corner |
| 270 | and point is at the lower right corner." |
| 271 | (let ((lines rectangle) |
| 272 | (insertcolumn (current-column)) |
| 273 | (first t)) |
| 274 | (push-mark) |
| 275 | (while lines |
| 276 | (or first |
| 277 | (progn |
| 278 | (forward-line 1) |
| 279 | (or (bolp) (insert ?\n)) |
| 280 | (move-to-column-force insertcolumn))) |
| 281 | (setq first nil) |
| 282 | (insert (car lines)) |
| 283 | (setq lines (cdr lines))))) |
| 284 | |
| 285 | ;;;###autoload |
| 286 | (defun open-rectangle (start end &optional fill) |
| 287 | "Blank out the region-rectangle, shifting text right. |
| 288 | |
| 289 | The text previously in the region is not overwritten by the blanks, |
| 290 | but instead winds up to the right of the rectangle. |
| 291 | |
| 292 | When called from a program the rectangle's corners are START and END. |
| 293 | With a prefix (or a FILL) argument, fill with blanks even if there is no text |
| 294 | on the right side of the rectangle." |
| 295 | (interactive "*r\nP") |
| 296 | (apply-on-rectangle 'open-rectangle-line start end fill) |
| 297 | (goto-char start)) |
| 298 | |
| 299 | (defun open-rectangle-line (startcol endcol fill) |
| 300 | (when (= (move-to-column-force startcol (or fill 'coerce)) startcol) |
| 301 | (unless (and (not fill) |
| 302 | (= (point) (point-at-eol))) |
| 303 | (indent-to endcol)))) |
| 304 | |
| 305 | (defun delete-whitespace-rectangle-line (startcol endcol fill) |
| 306 | (when (= (move-to-column-force startcol (or fill 'coerce)) startcol) |
| 307 | (unless (= (point) (point-at-eol)) |
| 308 | (delete-region (point) (progn (skip-syntax-forward " ") (point)))))) |
| 309 | |
| 310 | ;;;###autoload (defalias 'close-rectangle 'delete-whitespace-rectangle) ;; Old name |
| 311 | ;;;###autoload |
| 312 | (defun delete-whitespace-rectangle (start end &optional fill) |
| 313 | "Delete all whitespace following a specified column in each line. |
| 314 | The left edge of the rectangle specifies the position in each line |
| 315 | at which whitespace deletion should begin. On each line in the |
| 316 | rectangle, all continuous whitespace starting at that column is deleted. |
| 317 | |
| 318 | When called from a program the rectangle's corners are START and END. |
| 319 | With a prefix (or a FILL) argument, also fill too short lines." |
| 320 | (interactive "*r\nP") |
| 321 | (apply-on-rectangle 'delete-whitespace-rectangle-line start end fill)) |
| 322 | |
| 323 | ;; not used any more --dv |
| 324 | ;; string-rectangle uses this variable to pass the string |
| 325 | ;; to string-rectangle-line. |
| 326 | (defvar string-rectangle-string) |
| 327 | |
| 328 | (defun string-rectangle-line (startcol endcol string delete) |
| 329 | (move-to-column-force startcol) |
| 330 | (if delete |
| 331 | (delete-rectangle-line startcol endcol nil)) |
| 332 | (insert string)) |
| 333 | |
| 334 | ;;;###autoload |
| 335 | (defun string-rectangle (start end string) |
| 336 | "Replace rectangle contents with STRING on each line. |
| 337 | The length of STRING need not be the same as the rectangle width. |
| 338 | |
| 339 | Called from a program, takes three args; START, END and STRING." |
| 340 | (interactive "*r\nsString rectangle: ") |
| 341 | (apply-on-rectangle 'string-rectangle-line start end string t)) |
| 342 | |
| 343 | (defalias 'replace-rectangle 'string-rectangle) |
| 344 | |
| 345 | ;;;###autoload |
| 346 | (defun string-insert-rectangle (start end string) |
| 347 | "Insert STRING on each line of region-rectangle, shifting text right. |
| 348 | |
| 349 | When called from a program, the rectangle's corners are START and END. |
| 350 | The left edge of the rectangle specifies the column for insertion. |
| 351 | This command does not delete or overwrite any existing text." |
| 352 | (interactive "*r\nsString insert rectangle: ") |
| 353 | (apply-on-rectangle 'string-rectangle-line start end string nil)) |
| 354 | |
| 355 | ;;;###autoload |
| 356 | (defun clear-rectangle (start end &optional fill) |
| 357 | "Blank out the region-rectangle. |
| 358 | The text previously in the region is overwritten with blanks. |
| 359 | |
| 360 | When called from a program the rectangle's corners are START and END. |
| 361 | With a prefix (or a FILL) argument, also fill with blanks the parts of the |
| 362 | rectangle which were empty." |
| 363 | (interactive "*r\nP") |
| 364 | (apply-on-rectangle 'clear-rectangle-line start end fill)) |
| 365 | |
| 366 | (defun clear-rectangle-line (startcol endcol fill) |
| 367 | (let ((pt (point-at-eol)) |
| 368 | spaces) |
| 369 | (when (= (move-to-column-force startcol (or fill 'coerce)) startcol) |
| 370 | (if (and (not fill) |
| 371 | (<= (save-excursion (goto-char pt) (current-column)) endcol)) |
| 372 | (delete-region (point) pt) |
| 373 | ;; else |
| 374 | (setq pt (point)) |
| 375 | (move-to-column-force endcol) |
| 376 | (setq spaces (- (point) pt)) |
| 377 | (delete-region pt (point)) |
| 378 | (indent-to (+ (current-column) spaces)))))) |
| 379 | |
| 380 | (provide 'rect) |
| 381 | |
| 382 | ;;; rect.el ends here |