-(defun clear-rectangle (start end)
- "Blank out rectangle with corners at point and mark.
-The text previously in the region is overwritten by the blanks.
-When called from a program, requires two args which specify the corners."
- (interactive "r")
- (operate-on-rectangle 'clear-rectangle-line start end t))
-
-(defun clear-rectangle-line (startpos begextra endextra)
- ;; Find end of whitespace after the rectangle.
- (skip-chars-forward " \t")
- (let ((column (+ (current-column) endextra)))
- ;; Delete the text in the rectangle, and following whitespace.
- (delete-region (point)
- (progn (goto-char startpos)
- (skip-chars-backward " \t")
- (point)))
- ;; Reindent out to same column that we were at.
- (indent-to column)))
+(defalias 'replace-rectangle 'string-rectangle)
+
+;;;###autoload
+(defun string-insert-rectangle (start end string)
+ "Insert STRING on each line of region-rectangle, shifting text right.
+
+When called from a program, the rectangle's corners are START and END.
+The left edge of the rectangle specifies the column for insertion.
+This command does not delete or overwrite any existing text."
+ (interactive
+ (progn (barf-if-buffer-read-only)
+ (list
+ (region-beginning)
+ (region-end)
+ (read-string (format "String insert rectangle (default %s): "
+ (or (car string-rectangle-history) ""))
+ nil 'string-rectangle-history
+ (car string-rectangle-history)))))
+ (apply-on-rectangle 'string-rectangle-line start end string nil))
+
+;;;###autoload
+(defun clear-rectangle (start end &optional fill)
+ "Blank out the region-rectangle.
+The text previously in the region is overwritten with blanks.
+
+When called from a program the rectangle's corners are START and END.
+With a prefix (or a FILL) argument, also fill with blanks the parts of the
+rectangle which were empty."
+ (interactive "*r\nP")
+ (apply-on-rectangle 'clear-rectangle-line start end fill))
+
+(defun clear-rectangle-line (startcol endcol fill)
+ (let ((pt (point-at-eol)))
+ (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
+ (if (and (not fill)
+ (<= (save-excursion (goto-char pt) (current-column)) endcol))
+ (delete-region (point) pt)
+ ;; else
+ (setq pt (point))
+ (move-to-column endcol t)
+ (setq endcol (current-column))
+ (delete-region pt (point))
+ (indent-to endcol)))))
+
+;; Line numbers for `rectangle-number-line-callback'.
+(defvar rectangle-number-line-counter)
+
+(defun rectangle-number-line-callback (start _end format-string)
+ (move-to-column start t)
+ (insert (format format-string rectangle-number-line-counter))
+ (setq rectangle-number-line-counter
+ (1+ rectangle-number-line-counter)))
+
+(defun rectange--default-line-number-format (start end start-at)
+ (concat "%"
+ (int-to-string (length (int-to-string (+ (count-lines start end)
+ start-at))))
+ "d "))
+
+;;;###autoload
+(defun rectangle-number-lines (start end start-at &optional format)
+ "Insert numbers in front of the region-rectangle.
+
+START-AT, if non-nil, should be a number from which to begin
+counting. FORMAT, if non-nil, should be a format string to pass
+to `format' along with the line count. When called interactively
+with a prefix argument, prompt for START-AT and FORMAT."
+ (interactive
+ (if current-prefix-arg
+ (let* ((start (region-beginning))
+ (end (region-end))
+ (start-at (read-number "Number to count from: " 1)))
+ (list start end start-at
+ (read-string "Format string: "
+ (rectange--default-line-number-format
+ start end start-at))))
+ (list (region-beginning) (region-end) 1 nil)))
+ (unless format
+ (setq format (rectange--default-line-number-format start end start-at)))
+ (let ((rectangle-number-line-counter start-at))
+ (apply-on-rectangle 'rectangle-number-line-callback
+ start end format)))