1 ;;; fuel-table.el -- table creation
3 ;; Copyright (C) 2009 Jose Antonio Ortega Ruiz
4 ;; See http://factorcode.org/license.txt for BSD license.
6 ;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
7 ;; Keywords: languages, fuel, factor
8 ;; Start date: Tue Jan 06, 2009 13:44
12 ;; Utilities to insert ascii tables.
16 (defun fuel-table--col-widths (rows)
17 (let* ((col-no (length (car rows
)))
18 (available (- (window-width) 2 (* 2 col-no
)))
23 (av-width (- available
(* 5 (- col-no c
)))))
27 (max width
(length (nth c row
))))))
29 (setq available
(- available width
)))
33 (defun fuel-table--pad-str (str width
)
34 (let ((len (length str
)))
35 (cond ((= len width
) str
)
36 ((> len width
) (concat (substring str
0 (- width
3)) "..."))
37 (t (concat str
(make-string (- width
(length str
)) ?\
))))))
39 (defun fuel-table--str-lines (str width
)
40 (if (<= (length str
) width
)
41 (list (fuel-table--pad-str str width
))
43 (let ((fill-column width
))
45 (fill-region (point-min) (point-max))
46 (mapcar '(lambda (s) (fuel-table--pad-str s width
))
47 (split-string (buffer-string) "\n"))))))
49 (defun fuel-table--pad-row (row)
50 (let* ((max-ln (apply 'max
(mapcar 'length row
)))
53 (let ((ln (length lines
)))
54 (if (= ln max-ln
) (push lines result
)
55 (let ((lines (reverse lines
))
57 (blank (make-string (length (car lines
)) ?\
)))
61 (push (reverse lines
) result
)))))
64 (defun fuel-table--format-rows (rows widths
)
65 (let ((col-no (length (car rows
)))
70 (push (fuel-table--str-lines (nth c row
) (nth c widths
)) frow
)
72 (push (fuel-table--pad-row (reverse frow
)) frows
)))
75 (defun fuel-table--insert (rows)
76 (let* ((widths (fuel-table--col-widths rows
))
77 (rows (fuel-table--format-rows rows widths
))
78 (ls (concat "+" (mapconcat (lambda (n) (make-string n ?-
)) widths
"-+") "-+")))
81 (let ((ln (length (car r
)))
84 (insert (concat "|" (mapconcat 'identity
85 (mapcar `(lambda (x) (nth ,l x
)) r
)
93 ;;; fuel-table.el ends here