clinspect: Smarter sorting function.
parent
25ba3e0868
commit
eb924939c6
|
@ -1,12 +1,26 @@
|
||||||
|
;;; package-cl-inspect-mode.el --- -*- lexical-binding: t -*-
|
||||||
|
|
||||||
;; TODO: Name? cl-inspect? clinspect? clidget? eclidget?
|
;; TODO: Name? cl-inspect? clinspect? clidget? eclidget?
|
||||||
;; Actually, make it independent of CL.
|
;; Actually, make it independent of CL.
|
||||||
;; TODO: Better table abstraction?
|
;; TODO: Better table abstraction?
|
||||||
;; Maybe https://github.com/kiwanami/emacs-ctable?
|
;; Maybe https://github.com/kiwanami/emacs-ctable?
|
||||||
|
|
||||||
|
;; TODO: Add filters.
|
||||||
|
;; Can we add filters that ask for a column and a range depending on the type of data?
|
||||||
|
|
||||||
(require 'cl-lib)
|
(require 'cl-lib)
|
||||||
|
|
||||||
(defvar clinspect-column-max-width 40)
|
(defvar clinspect-column-max-width 40)
|
||||||
|
|
||||||
|
(defun clinspect--tabulated-list-sorter (entry index)
|
||||||
|
"Return a sorter that smarter than `tabulated-list' default.
|
||||||
|
If ENTRY element at INDEX is a number, sort by number, otherwise sort by string."
|
||||||
|
(if (numberp (nth index entry))
|
||||||
|
(lambda (a b)
|
||||||
|
(< (string-to-number (aref (cadr a) index))
|
||||||
|
(string-to-number (aref (cadr b) index))))
|
||||||
|
t))
|
||||||
|
|
||||||
(defun clinspect--tabulated-list-format (header entries)
|
(defun clinspect--tabulated-list-format (header entries)
|
||||||
(apply #'vector (mapcar (lambda (index)
|
(apply #'vector (mapcar (lambda (index)
|
||||||
(let ((column (nth index header)))
|
(let ((column (nth index header)))
|
||||||
|
@ -18,29 +32,19 @@
|
||||||
(length (prin1-to-string
|
(length (prin1-to-string
|
||||||
(nth index entry))))
|
(nth index entry))))
|
||||||
entries))))
|
entries))))
|
||||||
t
|
(clinspect--tabulated-list-sorter (car entries) index))))
|
||||||
;; (if (numberp (nth index (car data)))
|
|
||||||
;; (lambda (a b)
|
|
||||||
;; (< (nth index a)
|
|
||||||
;; (nth index b)))
|
|
||||||
;; t)
|
|
||||||
)))
|
|
||||||
(number-sequence 0 (1- (length header))))))
|
(number-sequence 0 (1- (length header))))))
|
||||||
|
|
||||||
(define-derived-mode clinspect-mode tabulated-list-mode "Clinspect"
|
(define-derived-mode clinspect-mode tabulated-list-mode "Clinspect"
|
||||||
"Mode to inspect Common Lisp sequences."
|
"Mode to inspect Common Lisp sequences."
|
||||||
;; (setq tabulated-list-padding 2)
|
|
||||||
;; (setq tabulated-list-sort-key (cons "Size" 'flip))
|
|
||||||
;; (setq tabulated-list-printer #'disk-usage--print-entry)
|
|
||||||
(add-hook 'tabulated-list-revert-hook 'tabulated-list-init-header nil t))
|
(add-hook 'tabulated-list-revert-hook 'tabulated-list-init-header nil t))
|
||||||
|
|
||||||
(defvar clinspect-buffer-name "clinspector")
|
(defvar clinspect-buffer-name "clinspector")
|
||||||
|
|
||||||
;; Input:
|
|
||||||
;; - header: list of strings
|
|
||||||
;; - ddata: list of list of things.
|
|
||||||
(defun clinspect (header data &optional name)
|
(defun clinspect (header data &optional name)
|
||||||
"Inspect DATA."
|
"Inspect DATA.
|
||||||
|
DATA is a list of things.
|
||||||
|
HEADER is a list of strings, the column names."
|
||||||
(switch-to-buffer
|
(switch-to-buffer
|
||||||
(generate-new-buffer (format "*%s%s*" clinspect-buffer-name
|
(generate-new-buffer (format "*%s%s*" clinspect-buffer-name
|
||||||
(if name
|
(if name
|
||||||
|
|
Loading…
Reference in New Issue