| 1 | ;;; rect.el --- rectangle functions for GNU Emacs |
| 2 | |
| 3 | ;; Copyright (C) 1985, 1999-2011 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Maintainer: Didier Verna <didier@xemacs.org> |
| 6 | ;; Keywords: internal |
| 7 | ;; Package: emacs |
| 8 | |
| 9 | ;; This file is part of GNU Emacs. |
| 10 | |
| 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 12 | ;; it under the terms of the GNU General Public License as published by |
| 13 | ;; the Free Software Foundation, either version 3 of the License, or |
| 14 | ;; (at your option) any later version. |
| 15 | |
| 16 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | ;; GNU General Public License for more details. |
| 20 | |
| 21 | ;; You should have received a copy of the GNU General Public License |
| 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | ;;; Commentary: |
| 25 | |
| 26 | ;; This package provides the operations on rectangles that are documented |
| 27 | ;; in the Emacs manual. |
| 28 | |
| 29 | ;; ### NOTE: this file was almost completely rewritten by Didier Verna |
| 30 | ;; <didier@xemacs.org> in July 1999. |
| 31 | |
| 32 | ;;; Global key bindings |
| 33 | |
| 34 | ;;;###autoload (define-key ctl-x-r-map "c" 'clear-rectangle) |
| 35 | ;;;###autoload (define-key ctl-x-r-map "k" 'kill-rectangle) |
| 36 | ;;;###autoload (define-key ctl-x-r-map "d" 'delete-rectangle) |
| 37 | ;;;###autoload (define-key ctl-x-r-map "y" 'yank-rectangle) |
| 38 | ;;;###autoload (define-key ctl-x-r-map "o" 'open-rectangle) |
| 39 | ;;;###autoload (define-key ctl-x-r-map "t" 'string-rectangle) |
| 40 | ;;;###autoload (define-key ctl-x-r-map "N" 'rectangle-number-lines) |
| 41 | |
| 42 | ;;; Code: |
| 43 | |
| 44 | ;; FIXME: this function should be replaced by `apply-on-rectangle' |
| 45 | (defun operate-on-rectangle (function start end coerce-tabs) |
| 46 | "Call FUNCTION for each line of rectangle with corners at START, END. |
| 47 | If COERCE-TABS is non-nil, convert multi-column characters |
| 48 | that span the starting or ending columns on any line |
| 49 | to multiple spaces before calling FUNCTION. |
| 50 | FUNCTION is called with three arguments: |
| 51 | position of start of segment of this line within the rectangle, |
| 52 | number of columns that belong to rectangle but are before that position, |
| 53 | number of columns that belong to rectangle but are after point. |
| 54 | Point is at the end of the segment of this line within the rectangle." |
| 55 | (let (startcol startlinepos endcol endlinepos) |
| 56 | (save-excursion |
| 57 | (goto-char start) |
| 58 | (setq startcol (current-column)) |
| 59 | (beginning-of-line) |
| 60 | (setq startlinepos (point))) |
| 61 | (save-excursion |
| 62 | (goto-char end) |
| 63 | (setq endcol (current-column)) |
| 64 | (forward-line 1) |
| 65 | (setq endlinepos (point-marker))) |
| 66 | (if (< endcol startcol) |
| 67 | (setq startcol (prog1 endcol (setq endcol startcol)))) |
| 68 | (save-excursion |
| 69 | (goto-char startlinepos) |
| 70 | (while (< (point) endlinepos) |
| 71 | (let (startpos begextra endextra) |
| 72 | (if coerce-tabs |
| 73 | (move-to-column startcol t) |
| 74 | (move-to-column startcol)) |
| 75 | (setq begextra (- (current-column) startcol)) |
| 76 | (setq startpos (point)) |
| 77 | (if coerce-tabs |
| 78 | (move-to-column endcol t) |
| 79 | (move-to-column endcol)) |
| 80 | ;; If we overshot, move back one character |
| 81 | ;; so that endextra will be positive. |
| 82 | (if (and (not coerce-tabs) (> (current-column) endcol)) |
| 83 | (backward-char 1)) |
| 84 | (setq endextra (- endcol (current-column))) |
| 85 | (if (< begextra 0) |
| 86 | (setq endextra (+ endextra begextra) |
| 87 | begextra 0)) |
| 88 | (funcall function startpos begextra endextra)) |
| 89 | (forward-line 1))) |
| 90 | (- endcol startcol))) |
| 91 | |
| 92 | (defun apply-on-rectangle (function start end &rest args) |
| 93 | "Call FUNCTION for each line of rectangle with corners at START, END. |
| 94 | FUNCTION is called with two arguments: the start and end columns of the |
| 95 | rectangle, plus ARGS extra arguments. Point is at the beginning of line when |
| 96 | the function is called." |
| 97 | (let (startcol startpt endcol endpt) |
| 98 | (save-excursion |
| 99 | (goto-char start) |
| 100 | (setq startcol (current-column)) |
| 101 | (beginning-of-line) |
| 102 | (setq startpt (point)) |
| 103 | (goto-char end) |
| 104 | (setq endcol (current-column)) |
| 105 | (forward-line 1) |
| 106 | (setq endpt (point-marker)) |
| 107 | ;; ensure the start column is the left one. |
| 108 | (if (< endcol startcol) |
| 109 | (let ((col startcol)) |
| 110 | (setq startcol endcol endcol col))) |
| 111 | ;; start looping over lines |
| 112 | (goto-char startpt) |
| 113 | (while (< (point) endpt) |
| 114 | (apply function startcol endcol args) |
| 115 | (forward-line 1))) |
| 116 | )) |
| 117 | |
| 118 | (defun delete-rectangle-line (startcol endcol fill) |
| 119 | (when (= (move-to-column startcol (if fill t 'coerce)) startcol) |
| 120 | (delete-region (point) |
| 121 | (progn (move-to-column endcol 'coerce) |
| 122 | (point))))) |
| 123 | |
| 124 | (defun delete-extract-rectangle-line (startcol endcol lines fill) |
| 125 | (let ((pt (point-at-eol))) |
| 126 | (if (< (move-to-column startcol (if fill t 'coerce)) startcol) |
| 127 | (setcdr lines (cons (spaces-string (- endcol startcol)) |
| 128 | (cdr lines))) |
| 129 | ;; else |
| 130 | (setq pt (point)) |
| 131 | (move-to-column endcol t) |
| 132 | (setcdr lines (cons (filter-buffer-substring pt (point) t) (cdr lines)))) |
| 133 | )) |
| 134 | |
| 135 | ;; This is actually the only function that needs to do complicated |
| 136 | ;; stuff like what's happening in `operate-on-rectangle', because the |
| 137 | ;; buffer might be read-only. |
| 138 | (defun extract-rectangle-line (startcol endcol lines) |
| 139 | (let (start end begextra endextra line) |
| 140 | (move-to-column startcol) |
| 141 | (setq start (point) |
| 142 | begextra (- (current-column) startcol)) |
| 143 | (move-to-column endcol) |
| 144 | (setq end (point) |
| 145 | endextra (- endcol (current-column))) |
| 146 | (setq line (buffer-substring start (point))) |
| 147 | (if (< begextra 0) |
| 148 | (setq endextra (+ endextra begextra) |
| 149 | begextra 0)) |
| 150 | (if (< endextra 0) |
| 151 | (setq endextra 0)) |
| 152 | (goto-char start) |
| 153 | (while (search-forward "\t" end t) |
| 154 | (let ((width (- (current-column) |
| 155 | (save-excursion (forward-char -1) |
| 156 | (current-column))))) |
| 157 | (setq line (concat (substring line 0 (- (point) end 1)) |
| 158 | (spaces-string width) |
| 159 | (substring line (+ (length line) |
| 160 | (- (point) end))))))) |
| 161 | (if (or (> begextra 0) (> endextra 0)) |
| 162 | (setq line (concat (spaces-string begextra) |
| 163 | line |
| 164 | (spaces-string endextra)))) |
| 165 | (setcdr lines (cons line (cdr lines))))) |
| 166 | |
| 167 | (defconst spaces-strings |
| 168 | '["" " " " " " " " " " " " " " " " "]) |
| 169 | |
| 170 | (defun spaces-string (n) |
| 171 | "Return a string with N spaces." |
| 172 | (if (<= n 8) (aref spaces-strings n) |
| 173 | (make-string n ?\s))) |
| 174 | |
| 175 | ;;;###autoload |
| 176 | (defun delete-rectangle (start end &optional fill) |
| 177 | "Delete (don't save) text in the region-rectangle. |
| 178 | The same range of columns is deleted in each line starting with the |
| 179 | line where the region begins and ending with the line where the region |
| 180 | ends. |
| 181 | |
| 182 | When called from a program the rectangle's corners are START and END. |
| 183 | With a prefix (or a FILL) argument, also fill lines where nothing has |
| 184 | to be deleted." |
| 185 | (interactive "*r\nP") |
| 186 | (apply-on-rectangle 'delete-rectangle-line start end fill)) |
| 187 | |
| 188 | ;;;###autoload |
| 189 | (defun delete-extract-rectangle (start end &optional fill) |
| 190 | "Delete the contents of the rectangle with corners at START and END. |
| 191 | Return it as a list of strings, one for each line of the rectangle. |
| 192 | |
| 193 | When called from a program the rectangle's corners are START and END. |
| 194 | With an optional FILL argument, also fill lines where nothing has to be |
| 195 | deleted." |
| 196 | (let ((lines (list nil))) |
| 197 | (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill) |
| 198 | (nreverse (cdr lines)))) |
| 199 | |
| 200 | ;;;###autoload |
| 201 | (defun extract-rectangle (start end) |
| 202 | "Return the contents of the rectangle with corners at START and END. |
| 203 | Return it as a list of strings, one for each line of the rectangle." |
| 204 | (let ((lines (list nil))) |
| 205 | (apply-on-rectangle 'extract-rectangle-line start end lines) |
| 206 | (nreverse (cdr lines)))) |
| 207 | |
| 208 | (defvar killed-rectangle nil |
| 209 | "Rectangle for `yank-rectangle' to insert.") |
| 210 | |
| 211 | ;;;###autoload |
| 212 | (defun kill-rectangle (start end &optional fill) |
| 213 | "Delete the region-rectangle and save it as the last killed one. |
| 214 | |
| 215 | When called from a program the rectangle's corners are START and END. |
| 216 | You might prefer to use `delete-extract-rectangle' from a program. |
| 217 | |
| 218 | With a prefix (or a FILL) argument, also fill lines where nothing has to be |
| 219 | deleted. |
| 220 | |
| 221 | If the buffer is read-only, Emacs will beep and refrain from deleting |
| 222 | the rectangle, but put it in the kill ring anyway. This means that |
| 223 | you can use this command to copy text from a read-only buffer. |
| 224 | \(If the variable `kill-read-only-ok' is non-nil, then this won't |
| 225 | even beep.)" |
| 226 | (interactive "r\nP") |
| 227 | (condition-case nil |
| 228 | (setq killed-rectangle (delete-extract-rectangle start end fill)) |
| 229 | ((buffer-read-only text-read-only) |
| 230 | (setq killed-rectangle (extract-rectangle start end)) |
| 231 | (if kill-read-only-ok |
| 232 | (progn (message "Read only text copied to kill ring") nil) |
| 233 | (barf-if-buffer-read-only) |
| 234 | (signal 'text-read-only (list (current-buffer))))))) |
| 235 | |
| 236 | ;;;###autoload |
| 237 | (defun yank-rectangle () |
| 238 | "Yank the last killed rectangle with upper left corner at point." |
| 239 | (interactive "*") |
| 240 | (insert-rectangle killed-rectangle)) |
| 241 | |
| 242 | ;;;###autoload |
| 243 | (defun insert-rectangle (rectangle) |
| 244 | "Insert text of RECTANGLE with upper left corner at point. |
| 245 | RECTANGLE's first line is inserted at point, its second |
| 246 | line is inserted at a point vertically under point, etc. |
| 247 | RECTANGLE should be a list of strings. |
| 248 | After this command, the mark is at the upper left corner |
| 249 | and point is at the lower right corner." |
| 250 | (let ((lines rectangle) |
| 251 | (insertcolumn (current-column)) |
| 252 | (first t)) |
| 253 | (push-mark) |
| 254 | (while lines |
| 255 | (or first |
| 256 | (progn |
| 257 | (forward-line 1) |
| 258 | (or (bolp) (insert ?\n)) |
| 259 | (move-to-column insertcolumn t))) |
| 260 | (setq first nil) |
| 261 | (insert-for-yank (car lines)) |
| 262 | (setq lines (cdr lines))))) |
| 263 | |
| 264 | ;;;###autoload |
| 265 | (defun open-rectangle (start end &optional fill) |
| 266 | "Blank out the region-rectangle, shifting text right. |
| 267 | |
| 268 | The text previously in the region is not overwritten by the blanks, |
| 269 | but instead winds up to the right of the rectangle. |
| 270 | |
| 271 | When called from a program the rectangle's corners are START and END. |
| 272 | With a prefix (or a FILL) argument, fill with blanks even if there is |
| 273 | no text on the right side of the rectangle." |
| 274 | (interactive "*r\nP") |
| 275 | (apply-on-rectangle 'open-rectangle-line start end fill) |
| 276 | (goto-char start)) |
| 277 | |
| 278 | (defun open-rectangle-line (startcol endcol fill) |
| 279 | (when (= (move-to-column startcol (if fill t 'coerce)) startcol) |
| 280 | (unless (and (not fill) |
| 281 | (= (point) (point-at-eol))) |
| 282 | (indent-to endcol)))) |
| 283 | |
| 284 | (defun delete-whitespace-rectangle-line (startcol endcol fill) |
| 285 | (when (= (move-to-column startcol (if fill t 'coerce)) startcol) |
| 286 | (unless (= (point) (point-at-eol)) |
| 287 | (delete-region (point) (progn (skip-syntax-forward " ") (point)))))) |
| 288 | |
| 289 | ;;;###autoload |
| 290 | (defalias 'close-rectangle 'delete-whitespace-rectangle) ;; Old name |
| 291 | |
| 292 | ;;;###autoload |
| 293 | (defun delete-whitespace-rectangle (start end &optional fill) |
| 294 | "Delete all whitespace following a specified column in each line. |
| 295 | The left edge of the rectangle specifies the position in each line |
| 296 | at which whitespace deletion should begin. On each line in the |
| 297 | rectangle, all continuous whitespace starting at that column is deleted. |
| 298 | |
| 299 | When called from a program the rectangle's corners are START and END. |
| 300 | With a prefix (or a FILL) argument, also fill too short lines." |
| 301 | (interactive "*r\nP") |
| 302 | (apply-on-rectangle 'delete-whitespace-rectangle-line start end fill)) |
| 303 | |
| 304 | (defvar string-rectangle-history nil) |
| 305 | (defun string-rectangle-line (startcol endcol string delete) |
| 306 | (move-to-column startcol t) |
| 307 | (if delete |
| 308 | (delete-rectangle-line startcol endcol nil)) |
| 309 | (insert string)) |
| 310 | |
| 311 | ;;;###autoload |
| 312 | (defun string-rectangle (start end string) |
| 313 | "Replace rectangle contents with STRING on each line. |
| 314 | The length of STRING need not be the same as the rectangle width. |
| 315 | |
| 316 | Called from a program, takes three args; START, END and STRING." |
| 317 | (interactive |
| 318 | (progn (barf-if-buffer-read-only) |
| 319 | (list |
| 320 | (region-beginning) |
| 321 | (region-end) |
| 322 | (read-string (format "String rectangle (default %s): " |
| 323 | (or (car string-rectangle-history) "")) |
| 324 | nil 'string-rectangle-history |
| 325 | (car string-rectangle-history))))) |
| 326 | (apply-on-rectangle 'string-rectangle-line start end string t)) |
| 327 | |
| 328 | ;;;###autoload |
| 329 | (defalias 'replace-rectangle 'string-rectangle) |
| 330 | |
| 331 | ;;;###autoload |
| 332 | (defun string-insert-rectangle (start end string) |
| 333 | "Insert STRING on each line of region-rectangle, shifting text right. |
| 334 | |
| 335 | When called from a program, the rectangle's corners are START and END. |
| 336 | The left edge of the rectangle specifies the column for insertion. |
| 337 | This command does not delete or overwrite any existing text." |
| 338 | (interactive |
| 339 | (progn (barf-if-buffer-read-only) |
| 340 | (list |
| 341 | (region-beginning) |
| 342 | (region-end) |
| 343 | (read-string (format "String insert rectangle (default %s): " |
| 344 | (or (car string-rectangle-history) "")) |
| 345 | nil 'string-rectangle-history |
| 346 | (car string-rectangle-history))))) |
| 347 | (apply-on-rectangle 'string-rectangle-line start end string nil)) |
| 348 | |
| 349 | ;;;###autoload |
| 350 | (defun clear-rectangle (start end &optional fill) |
| 351 | "Blank out the region-rectangle. |
| 352 | The text previously in the region is overwritten with blanks. |
| 353 | |
| 354 | When called from a program the rectangle's corners are START and END. |
| 355 | With a prefix (or a FILL) argument, also fill with blanks the parts of the |
| 356 | rectangle which were empty." |
| 357 | (interactive "*r\nP") |
| 358 | (apply-on-rectangle 'clear-rectangle-line start end fill)) |
| 359 | |
| 360 | (defun clear-rectangle-line (startcol endcol fill) |
| 361 | (let ((pt (point-at-eol))) |
| 362 | (when (= (move-to-column startcol (if fill t 'coerce)) startcol) |
| 363 | (if (and (not fill) |
| 364 | (<= (save-excursion (goto-char pt) (current-column)) endcol)) |
| 365 | (delete-region (point) pt) |
| 366 | ;; else |
| 367 | (setq pt (point)) |
| 368 | (move-to-column endcol t) |
| 369 | (setq endcol (current-column)) |
| 370 | (delete-region pt (point)) |
| 371 | (indent-to endcol))))) |
| 372 | |
| 373 | ;; Line numbers for `rectangle-number-line-callback'. |
| 374 | (defvar rectangle-number-line-counter) |
| 375 | |
| 376 | (defun rectangle-number-line-callback (start end format-string) |
| 377 | (move-to-column start t) |
| 378 | (insert (format format-string rectangle-number-line-counter)) |
| 379 | (setq rectangle-number-line-counter |
| 380 | (1+ rectangle-number-line-counter))) |
| 381 | |
| 382 | (defun rectange--default-line-number-format (start end start-at) |
| 383 | (concat "%" |
| 384 | (int-to-string (length (int-to-string (+ (count-lines start end) |
| 385 | start-at)))) |
| 386 | "d ")) |
| 387 | |
| 388 | ;;;###autoload |
| 389 | (defun rectangle-number-lines (start end start-at &optional format) |
| 390 | "Insert numbers in front of the region-rectangle. |
| 391 | |
| 392 | START-AT, if non-nil, should be a number from which to begin |
| 393 | counting. FORMAT, if non-nil, should be a format string to pass |
| 394 | to `format' along with the line count. When called interactively |
| 395 | with a prefix argument, prompt for START-AT and FORMAT." |
| 396 | (interactive |
| 397 | (if current-prefix-arg |
| 398 | (let* ((start (region-beginning)) |
| 399 | (end (region-end)) |
| 400 | (start-at (read-number "Number to count from: " 1))) |
| 401 | (list start end start-at |
| 402 | (read-string "Format string: " |
| 403 | (rectange--default-line-number-format |
| 404 | start end start-at)))) |
| 405 | (list (region-beginning) (region-end) 1 nil))) |
| 406 | (unless format |
| 407 | (setq format (rectange--default-line-number-format start end start-at))) |
| 408 | (let ((rectangle-number-line-counter start-at)) |
| 409 | (apply-on-rectangle 'rectangle-number-line-callback |
| 410 | start end format))) |
| 411 | |
| 412 | (provide 'rect) |
| 413 | |
| 414 | ;;; rect.el ends here |