Function: table-insert-row

table-insert-row is an autoloaded, interactive and byte-compiled function defined in table.el.gz.

Signature

(table-insert-row N)

Documentation

Insert N table row(s).

When point is in a table the newly inserted row(s) are placed above the current row. When point is outside of the table it must be below the table within the table width range, then the newly created row(s) are appended at the bottom of the table.

View in manual

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/table.el.gz
;;;###autoload
(defun table-insert-row (n)
  "Insert N table row(s).
When point is in a table the newly inserted row(s) are placed above
the current row.  When point is outside of the table it must be below
the table within the table width range, then the newly created row(s)
are appended at the bottom of the table."
  (interactive "*p")
  (if (< n 0) (setq n 1))
  (let* ((current-coordinate (table--get-coordinate))
	 (coord-list (table--cell-list-to-coord-list (table--horizontal-cell-list t nil 'top)))
	 (append-row (if coord-list nil (setq coord-list (table--find-row-column))))
	 (cell-height (cdr (table--min-coord-list coord-list)))
	 (left-list nil)
	 (this-list coord-list)
	 (right-list (cdr coord-list))
	 (bottom-border-y (1+ (cdr (table--get-coordinate (cdr (table--vertical-cell-list nil t))))))
	 (vertical-str (string table-cell-vertical-char))
	 (vertical-str-with-properties (let ((str (string table-cell-vertical-char)))
					 (table--put-cell-keymap-property 0 (length str) str)
					 (table--put-cell-rear-nonsticky 0 (length str) str) str))
	 (first-time t))
    ;; create the space below for the table to grow
    (table--create-growing-space-below (* n (+ 1 cell-height)) coord-list bottom-border-y)
    ;; vertically expand each cell from left to right
    (while this-list
      (let* ((left (prog1 (car left-list) (setq left-list (if left-list (cdr left-list) coord-list))))
	     (this (prog1 (car this-list) (setq this-list (cdr this-list))))
	     (right (prog1 (car right-list) (setq right-list (cdr right-list))))
	     (exclude-left (and left (< (cdar left) (cdar this))))
	     (exclude-right (and right (<= (cdar right) (cdar this))))
	     (beg (table--goto-coordinate
		   (cons (if exclude-left (caar this) (1- (caar this)))
			 (cdar this))))
	     (end (table--goto-coordinate
		   (cons (if exclude-right (cadr this) (1+ (cadr this)))
			 bottom-border-y)))
	     (rect (if append-row nil (extract-rectangle beg end))))
	;; prepend blank cell lines to the extracted rectangle
	(let ((i n))
	  (while (> i 0)
	    (setq rect (cons
			(concat (if exclude-left "" (char-to-string table-cell-intersection-char))
				(make-string (- (cadr this) (caar this)) (string-to-char table-cell-horizontal-chars))
				(if exclude-right "" (char-to-string table-cell-intersection-char)))
			rect))
	    (let ((j cell-height))
	      (while (> j 0)
		(setq rect (cons
			    (concat (if exclude-left ""
				      (if first-time vertical-str vertical-str-with-properties))
				    (table--cell-blank-str (- (cadr this) (caar this)))
				    (if exclude-right "" vertical-str-with-properties))
			    rect))
		(setq j (1- j))))
	    (setq i (1- i))))
	(setq first-time nil)
	(if append-row
	    (table--goto-coordinate (cons (if exclude-left (caar this) (1- (caar this)))
					  (1+ bottom-border-y)))
	  (delete-rectangle beg end)
	  (goto-char beg))
	(table--insert-rectangle rect)))
    ;; fix up the intersections
    (setq this-list (if append-row nil coord-list))
    (while this-list
      (let ((this (prog1 (car this-list) (setq this-list (cdr this-list))))
	    (i 0))
	(while (< i n)
	  (let ((y (1- (* i (+ 1 cell-height)))))
	    (table--goto-coordinate (table--offset-coordinate (car this) (cons -1  y)))
	    (delete-char 1) (insert table-cell-intersection-char)
	    (table--goto-coordinate (table--offset-coordinate (cons (cadr this) (cdar this)) (cons 0  y)))
	    (delete-char 1) (insert table-cell-intersection-char)
	    (setq i (1+ i))))))
    ;; move the point to the beginning of the first newly inserted cell.
    (if (table--goto-coordinate
	 (if append-row (cons (car (caar coord-list)) (1+ bottom-border-y))
	   (caar coord-list))) nil
      (table--goto-coordinate current-coordinate))
    ;; re-recognize the current cell's new dimension
    (table-recognize-cell 'force)))