Search code examples
emacslispelisp

How to create a column view in Emacs Lisp?


I'm writing my own mode in Elisp. It's basically a simple crud application showing rows of data which can be manipulated via the minibuffer. I'd like to create a view for these rows which looks like the emacs package manager: columns of data nicely aligned. What's the best way to implement such a view?


Solution

  • The answer from phils got me on track. There are no tutorials or simple examples anywhere though, so I created one. Here is an example of a tabulated-list-mode derivative which has static data and can print the ID of the current column:

    (define-derived-mode mymode tabulated-list-mode "mymode" "Major mode My Mode, just a test"
      (setq tabulated-list-format [("Col1" 18 t)
                                   ("Col2" 12 nil)
                                   ("Col3"  10 t)
                                   ("Col4" 0 nil)])
      (setq tabulated-list-padding 2)
      (setq tabulated-list-sort-key (cons "Col3" nil))
      (tabulated-list-init-header))
    
    (defun print-current-line-id ()
      (interactive)
       (message (concat "current line ID is: " (tabulated-list-get-id))))
    
    (defun my-listing-command ()
      (interactive)
      (pop-to-buffer "*MY MODE*" nil)
      (mymode)
      (setq tabulated-list-entries (list 
                    (list "1" ["1" "2" "3" "4"])
                    (list "2" ["a" "b" "c" "d"])))
      (tabulated-list-print t))