Function: table--detect-cell-alignment

table--detect-cell-alignment is a byte-compiled function defined in table.el.gz.

Signature

(table--detect-cell-alignment CELL)

Documentation

Detect CELL contents alignment.

Guess CELL contents alignment both horizontally and vertically by looking at the appearance of the CELL contents.

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/table.el.gz
(defun table--detect-cell-alignment (cell)
  "Detect CELL contents alignment.
Guess CELL contents alignment both horizontally and vertically by
looking at the appearance of the CELL contents."
  (let ((cell-contents (extract-rectangle (car cell) (cdr cell)))
	(left-margin 0)
	(right-margin 0)
	(top-margin 0)
	(bottom-margin 0)
	(margin-diff 0)
	(margin-info-available nil)
	justify valign)
    (with-temp-buffer
      (table--insert-rectangle cell-contents)
      ;; determine the horizontal justification
      (goto-char (point-min))
      (while (re-search-forward "^\\( *\\).*[^ \n]\\( *\\)$" nil t)
	(setq margin-info-available t)
	(let* ((lm (- (match-end 1) (match-beginning 1)))
	       (rm (- (match-end 2) (match-beginning 2)))
	       (md (abs (- lm rm))))
	  (if (> lm left-margin)
	      (setq left-margin lm))
	  (if (> rm right-margin)
	      (setq right-margin rm))
	  (if (> md margin-diff)
	      (setq margin-diff md))))
      (setq justify
	    (cond
	     ((and margin-info-available
		   (<= margin-diff 1)
		   (> left-margin 0)) 'center)
	     ((and margin-info-available
		   (zerop right-margin)
		   (> left-margin 0)) 'right)
	     (t 'left)))
      ;; determine the vertical justification
      (goto-char (point-min))
      (if (and (re-search-forward "\\s *\\S " nil t)
	       (/= (match-beginning 0) (match-end 0)))
	  (setq top-margin (1- (count-lines (match-beginning 0) (match-end 0)))))
      (if (and (re-search-forward "\\s *\\'" nil t)
	       (/= (match-beginning 0) (match-end 0)))
	  (setq bottom-margin (1- (count-lines (match-beginning 0) (match-end 0)))))
      (setq valign
	    (cond
	     ((and (> top-margin 0)
		   (> bottom-margin 0)
		   (<= (abs (- top-margin bottom-margin)) 1)) 'middle)
	     ((and (> top-margin 0)
		   (zerop bottom-margin)) 'bottom)
	     (t nil))))
    (table--put-cell-justify-property cell justify)
    (table--put-cell-valign-property cell valign)))