Function: table-generate-source

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

Signature

(table-generate-source LANGUAGE &optional DEST-BUFFER CAPTION)

Documentation

Generate source of the current table in the specified language.

LANGUAGE is a symbol that specifies the language to describe the structure of the table. It must be either html, latex, cals, wiki, or mediawiki. The function inserts the resulting source text into DEST-BUFFER, and returns the buffer object. When DEST-BUFFER is omitted or nil, the function uses the default buffer specified in table-dest-buffer-name. In this case, the function erases the default buffer prior to the source generation. When DEST-BUFFER is non-nil, it should be either a destination buffer or a name of the destination buffer. In that case, the function inserts the generated result at point in the destination buffer, and leaves the previous contents of the buffer untouched.

References used for this implementation:

HTML:
        URL https://www.w3.org

LaTeX:
        URL https://www.maths.tcd.ie/~dwilkins/LaTeXPrimer/Tables.html

CALS (DocBook DTD):
        URL https://www.oasis-open.org/html/a502.htm
        URL https://www.oreilly.com/catalog/docbook/chapter/book/table.html#AEN114751

View in manual

Probably introduced at or before Emacs version 27.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/table.el.gz
;;;###autoload
(defun table-generate-source (language &optional dest-buffer caption)
  "Generate source of the current table in the specified language.
LANGUAGE is a symbol that specifies the language to describe the
structure of the table.  It must be either `html', `latex', `cals',
`wiki', or `mediawiki'.
The function inserts the resulting source text into DEST-BUFFER, and
returns the buffer object.  When DEST-BUFFER is omitted or nil, the
function uses the default buffer specified in `table-dest-buffer-name'.
In this case, the function erases the default buffer prior to the
source generation.
When DEST-BUFFER is non-nil, it should be either a destination
buffer or a name of the destination buffer.  In that case, the
function inserts the generated result at point in the destination
buffer, and leaves the previous contents of the buffer untouched.

References used for this implementation:

HTML:
        URL `https://www.w3.org'

LaTeX:
        URL `https://www.maths.tcd.ie/~dwilkins/LaTeXPrimer/Tables.html'

CALS (DocBook DTD):
        URL `https://www.oasis-open.org/html/a502.htm'
        URL `https://www.oreilly.com/catalog/docbook/chapter/book/table.html#AEN114751'"
  (interactive
   (let* ((_ (unless (table--probe-cell) (error "Table not found here")))
	  (completion-ignore-case t)
	  (default (car table-source-language-history))
	  (language (downcase (completing-read
			       (format-prompt "Language" default)
			       table-source-languages
			       nil t nil 'table-source-language-history default))))
     (list
      (intern language)
      (read-buffer "Destination buffer: " (concat table-dest-buffer-name "." language))
      (table--read-from-minibuffer '("Table Caption" . table-source-caption-history)))))
  (let ((default-buffer-name (concat table-dest-buffer-name "." (symbol-name language))))
    (unless (or (called-interactively-p 'interactive) (table--probe-cell))
      (error "Table not found here"))
    (unless (bufferp dest-buffer)
      (setq dest-buffer (get-buffer-create (or dest-buffer default-buffer-name))))
    (if (string= (buffer-name dest-buffer) default-buffer-name)
	(with-current-buffer dest-buffer
	  (erase-buffer)))
    (save-excursion
      (let ((starting-cell (table--probe-cell))
	    cell origin-cell tail-cell col-list row-list (n 0) i)
	;; first analyze the table structure and prepare:
	;; 1. origin cell (left up corner cell)
	;; 2. tail cell (right bottom corner cell)
	;; 3. column boundary list
	;; 4. row boundary list
	(setq origin-cell starting-cell)
	(setq tail-cell starting-cell)
	(setq col-list (cons (car (table--get-coordinate (car starting-cell))) nil))
	(setq row-list (cons (cdr (table--get-coordinate (car starting-cell))) nil))
	(setq i 0)
	(let ((wheel [?- ?\\ ?| ?/]))
	  (while
	      (progn
		(if (called-interactively-p 'interactive)
		    (progn
		      (message "Analyzing table...%c" (aref wheel i))
		      (if (eq (setq i (1+ i)) (length wheel))
			  (setq i 0))
		      (setq n (1+ n))))
		(table-forward-cell 1 t)
		(and (setq cell (table--probe-cell))
		     (not (equal cell starting-cell))))
	    (if (< (car cell) (car origin-cell))
		(setq origin-cell cell))
	    (if (> (cdr cell) (cdr tail-cell))
		(setq tail-cell cell))
	    (let ((lu-coordinate (table--get-coordinate (car cell))))
	      (unless (memq (car lu-coordinate) col-list)
		(setq col-list (cons (car lu-coordinate) col-list)))
	      (unless (memq (cdr lu-coordinate) row-list)
		(setq row-list (cons (cdr lu-coordinate) row-list))))))
	(setq col-list (sort col-list #'<))
	(setq row-list (sort row-list #'<))
	(message "Generating source...")
	;; clear the source generation property list
	(setplist 'table-source-info-plist nil)
	;; prepare to start from the origin cell
	(goto-char (car origin-cell))
	;; first put some header information
	(table--generate-source-prologue dest-buffer language caption col-list row-list)
	(cond
	 ((eq language 'latex)
	  ;; scan by character lines
	  (table--generate-source-scan-lines dest-buffer language origin-cell tail-cell col-list row-list))
	 (t
	  ;; scan by table cells
	  (table--generate-source-scan-rows dest-buffer language origin-cell col-list row-list)))
	;; insert closing
	(table--generate-source-epilogue dest-buffer language col-list row-list))
      ;; lastly do some convenience work
      (if (called-interactively-p 'interactive)
	  (save-selected-window
	    (pop-to-buffer dest-buffer t)
	    (goto-char (point-min))
	    (and (string= (buffer-name dest-buffer) default-buffer-name)
		 (buffer-file-name dest-buffer)
		 (save-buffer))
	    (message "Generating source...done")
	    (let ((mode
		   (if (memq language '(cals)) 'sgml-mode
		     (intern (concat (symbol-name language) "-mode")))))
	      (if (fboundp mode)
		  (call-interactively mode)))
	    )))
    dest-buffer))