Function: org-table-convert-region

org-table-convert-region is an autoloaded, interactive and byte-compiled function defined in org-table.el.gz.

Signature

(org-table-convert-region BEG0 END0 &optional SEPARATOR)

Documentation

Convert region to a table.

The region goes from BEG0 to END0, but these borders will be moved slightly, to make sure a beginning of line in the first line is included.

Throw an error when the region has more than org-table-convert-region-max-lines lines.

SEPARATOR specifies the field separator in the lines. It can have the following values:

(4) Use the comma as a field separator
(16) Use a TAB as field separator
(64) Prompt for a regular expression as field separator
integer When a number, use that many spaces, or a TAB, as field separator
regexp When a regular expression, use it to match the separator
nil When nil, the command tries to be smart and figure out the
         separator in the following way:
         - when each line contains a TAB, assume TAB-separated material
         - when each line contains a comma, assume CSV material
         - else, assume one or more SPACE characters as separator.
babel-auto
       Use the same rules as nil, but do not try any separator when
       the region contains a single line and has no commas or tabs.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-table.el.gz
;;;###autoload
(defun org-table-convert-region (beg0 end0 &optional separator)
  "Convert region to a table.

The region goes from BEG0 to END0, but these borders will be moved
slightly, to make sure a beginning of line in the first line is
included.

Throw an error when the region has more than
`org-table-convert-region-max-lines' lines.

SEPARATOR specifies the field separator in the lines.  It can have the
following values:

(4)     Use the comma as a field separator
(16)    Use a TAB as field separator
(64)    Prompt for a regular expression as field separator
integer  When a number, use that many spaces, or a TAB, as field separator
regexp   When a regular expression, use it to match the separator
nil      When nil, the command tries to be smart and figure out the
         separator in the following way:
         - when each line contains a TAB, assume TAB-separated material
         - when each line contains a comma, assume CSV material
         - else, assume one or more SPACE characters as separator.
`babel-auto'
       Use the same rules as nil, but do not try any separator when
       the region contains a single line and has no commas or tabs."
  (interactive "r\nP")
  (let* ((beg (min beg0 end0))
	 (end (max beg0 end0))
	 re)
    (when (> (count-lines beg end) org-table-convert-region-max-lines)
      (user-error "Region is longer than `org-table-convert-region-max-lines' (%s) lines; not converting"
		  org-table-convert-region-max-lines))
    (when (equal separator '(64))
      (setq separator (read-regexp "Regexp for field separator")))
    (goto-char beg)
    (forward-line 0)
    (setq beg (point-marker))
    (goto-char end)
    (if (bolp) (backward-char 1) (end-of-line 1))
    (setq end (point-marker))
    ;; Get the right field separator
    (when (or (not separator) (eq separator 'babel-auto))
      (goto-char beg)
      (setq separator
	    (cond
	     ((not (save-excursion (re-search-forward "^[^\n\t]+$" end t))) '(16))
	     ((not (save-excursion (re-search-forward "^[^\n,]+$" end t))) '(4))
             ((and (eq separator 'babel-auto)
                   (= 1 (count-lines beg end)))
              (rx unmatchable))
	     (t 1))))
    (goto-char beg)
    (if (equal separator '(4))
	(while (< (point) end)
	  ;; parse the csv stuff
	  (cond
	   ((looking-at "^") (insert "| "))
	   ((looking-at "[ \t]*$") (replace-match " |") (forward-line 1))
	   ((looking-at "[ \t]*\"\\([^\"\n]*\\)\"")
	    (replace-match "\\1")
	    (if (looking-at "\"") (insert "\"")))
	   ((looking-at "[^,\n]+") (goto-char (match-end 0)))
	   ((looking-at "[ \t]*,") (replace-match " | "))
	   (t (forward-line 1))))
      (setq re (cond
		((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
		((equal separator '(16)) "^\\|\t")
		((integerp separator)
		 (if (< separator 1)
		     (user-error "Number of spaces in separator must be >= 1")
		   (format "^ *\\| *\t *\\| \\{%d,\\}" separator)))
		((stringp separator)
		 (format "^ *\\|%s" separator))
		(t (error "This should not happen"))))
      (while (re-search-forward re end t)
	(replace-match "| " t t)))
    (goto-char beg)
    (org-table-align)))