Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a column view in Emacs Lisp?

Tags:

emacs

lisp

elisp

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?

like image 482
auramo Avatar asked Jun 30 '12 08:06

auramo


2 Answers

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))
like image 93
auramo Avatar answered Nov 03 '22 15:11

auramo


If you look at the code for the package listing function you mention, you'll see that it employs package-menu-mode which derives from tabulated-list-mode.

  • M-x find-function RET package-menu-mode RET
  • C-hf tabulated-list-mode RET
like image 24
phils Avatar answered Nov 03 '22 14:11

phils