Function: org-table-get-range

org-table-get-range is a byte-compiled function defined in org-table.el.gz.

Signature

(org-table-get-range DESC &optional TBEG COL HIGHLIGHT CORNERS-ONLY)

Documentation

Get a calc vector from a column, according to descriptor DESC.

Optional arguments TBEG and COL can give the beginning of the table and the current column, to avoid unnecessary parsing.

HIGHLIGHT means just highlight the range.

When CORNERS-ONLY is set, only return the corners of the range as a list (line1 column1 line2 column2) where line1 and line2 are line numbers relative to beginning of table, or TBEG, and column1 and column2 are table column numbers.

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-table.el.gz
(defun org-table-get-range (desc &optional tbeg col highlight corners-only)
  "Get a calc vector from a column, according to descriptor DESC.

Optional arguments TBEG and COL can give the beginning of the table and
the current column, to avoid unnecessary parsing.

HIGHLIGHT means just highlight the range.

When CORNERS-ONLY is set, only return the corners of the range as
a list (line1 column1 line2 column2) where line1 and line2 are
line numbers relative to beginning of table, or TBEG, and column1
and column2 are table column numbers."
  (let* ((desc (if (string-match-p "\\`\\$[0-9]+\\.\\.\\$[0-9]+\\'" desc)
		   (replace-regexp-in-string "\\$" "@0$" desc)
		 desc))
	 (col (or col (org-table-current-column)))
	 (tbeg (or tbeg (org-table-begin)))
	 (thisline (count-lines tbeg (line-beginning-position))))
    (unless (string-match org-table-range-regexp desc)
      (user-error "Invalid table range specifier `%s'" desc))
    (let ((rangep (match-end 3))
	  (r1 (let ((r (and (match-end 1) (match-string 1 desc))))
		(or (save-match-data
		      (and (org-string-nw-p r)
			   (org-table--descriptor-line r thisline)))
		    thisline)))
	  (r2 (let ((r (and (match-end 4) (match-string 4 desc))))
		(or (save-match-data
		      (and (org-string-nw-p r)
			   (org-table--descriptor-line r thisline)))
		    thisline)))
	  (c1 (let ((c (and (match-end 2) (substring (match-string 2 desc) 1))))
		(if (or (not c) (= (string-to-number c) 0)) col
		  (+ (string-to-number c)
		     (if (memq (string-to-char c) '(?- ?+)) col 0)))))
	  (c2 (let ((c (and (match-end 5) (substring (match-string 5 desc) 1))))
		(if (or (not c) (= (string-to-number c) 0)) col
		  (+ (string-to-number c)
		     (if (memq (string-to-char c) '(?- ?+)) col 0))))))
      (save-excursion
	(if (and (not corners-only)
		 (or (not rangep) (and (= r1 r2) (= c1 c2))))
	    ;; Just one field.
	    (progn
	      (forward-line (- r1 thisline))
	      (while (not (looking-at org-table-dataline-regexp))
		(forward-line))
	      (prog1 (org-trim (org-table-get-field c1))
		(when highlight (org-table-highlight-rectangle))))
	  ;; A range, return a vector.  First sort the numbers to get
	  ;; a regular rectangle.
	  (let ((first-row (min r1 r2))
		(last-row (max r1 r2))
		(first-column (min c1 c2))
		(last-column (max c1 c2)))
	    (if corners-only (list first-row first-column last-row last-column)
	      ;; Copy the range values into a list.
	      (forward-line (- first-row thisline))
	      (while (not (looking-at org-table-dataline-regexp))
		(forward-line)
		(cl-incf first-row))
	      (org-table-goto-column first-column)
	      (let ((beg (point)))
		(forward-line (- last-row first-row))
		(while (not (looking-at org-table-dataline-regexp))
		  (forward-line -1))
		(org-table-goto-column last-column)
		(let ((end (point)))
		  (when highlight
		    (org-table-highlight-rectangle
		     beg (progn (skip-chars-forward "^|\n") (point))))
		  ;; Return string representation of calc vector.
		  (mapcar #'org-trim
			  (apply #'append
				 (org-table-copy-region beg end))))))))))))