Use tabulated-list-mode to create a widget to choose findlib package

Ignore-this: dae6384889fa21002d0669860d557122

darcs-hash:20120301014144-33bd9-97d020adaad5f924a807a1310a59b2a31b40a245
This commit is contained in:
wojciech.meyer 2012-03-01 02:41:44 +01:00
parent a186e64385
commit 6a5693772f
1 changed files with 51 additions and 29 deletions

View File

@ -750,37 +750,59 @@ defaults to 0."
lines) lines)
(nreverse packages)))) (nreverse packages))))
(defun utop-require () (define-derived-mode utop-list-packages-mode tabulated-list-mode "OCaml package list"
"Show the list of findlib packages." "Major mode for listing the findlib OCaml packages."
(setq tabulated-list-format [("Name" 32 t)
("Version" 32 t)])
(setq tabulated-list-sort-key (cons "Name" nil))
(setq tabulated-list-printer 'utop-package-printer)
(add-hook 'tabulated-list-revert-hook 'utop-list-packages--refresh nil t)
(tabulated-list-init-header))
(defun utop-list-packages--refresh ()
"Refresh the list of findlib packages."
(interactive) (interactive)
;; Clear up list of entries
(setq tabulated-list-entries nil)
;; Get the list of packages ;; Get the list of packages
(let ((packages (utop-ocamlfind-list-packages))) (let* ((packages (utop-ocamlfind-list-packages))
(save-excursion (max-name-length 0))
(with-output-to-temp-buffer "*Findlib packages*"
(set-buffer standard-output)
(let ((inhibit-read-only t))
(insert "Choose a findlib package to load:\n\n")
(let ((max-name-length 0))
;; Find the longest package name ;; Find the longest package name
(mapc (mapc
(lambda (package) (lambda (package)
(setq max-name-length (max max-name-length (length (car package))))) (setq max-name-length
(max max-name-length (length (car package)))))
packages) packages)
;; Minimal size of the name entry shall be 16 characters
(setq max-name-length (1+ (max max-name-length 16))) (setq max-name-length (1+ (max max-name-length 16)))
;; Insert headers ;; Set the header column size to the maximal length
(insert "Package name") (setcdr (elt tabulated-list-format 0) (list max-name-length t))
(insert-char 32 (- max-name-length 12)) ;; Build a list, accumulating in tabulated-list-entries
(insert "Version\n")
;; Insert buttons
(while packages (while packages
(let* ((package (car packages)) (let* ((package (car packages))
(name (car package)) (name (car package))
(version (cdr package))) (version (cdr package)))
(insert-text-button name 'face nil) (push (list package (vector name version))
(insert-char 32 (- max-name-length (length name))) tabulated-list-entries))
(insert version "\n")) (setq packages (cdr packages)))))
(setq packages (cdr packages)))
(goto-char (point-min)))))))) (defun utop-package-printer (id cols)
"Print one findlib package entry."
(let ((width (cadr (elt tabulated-list-format 0))))
(insert-text-button (elt cols 0) 'face nil)
(insert-char ?\s (- width (length (elt cols 0))))
(insert (elt cols 1) "\n")))
(defun list-ocaml-packages (&optional query-only buffer)
"Display a list of all ocaml findlib packages"
(interactive)
(unless (bufferp buffer)
(setq buffer (get-buffer-create "*Findlib packages*")))
(with-current-buffer buffer
(utop-list-packages-mode)
(utop-list-packages--refresh)
(tabulated-list-print t)
(display-buffer buffer)))
;; +-----------------------------------------------------------------+ ;; +-----------------------------------------------------------------+
;; | Menu | ;; | Menu |