[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: variable tabulated-list-format
From: |
Jean Louis |
Subject: |
Re: variable tabulated-list-format |
Date: |
Mon, 17 Oct 2022 07:17:18 +0300 |
User-agent: |
Mutt/2.2.7+37 (a90f69b) (2022-09-02) |
* John Haman <mail@johnhaman.org> [2022-10-17 01:43]:
> Hi everyone,
>
> I'm working on a mode that derives from tabulated-list-mode. My idea is to
> display small data.frames from ESS-R-mode using tabulated-list. As I may
> have several data.frames at a time, I need the header (column names) to vary
> with each call.
>
> Looking at the docs for tabulated-list-mode
> (https://www.gnu.org/software/emacs/manual/html_node/elisp/Tabulated-List-Mode.html),
> it looks like usage requires that my derived-mode specify the column names
> and meta-data (tabulated-lis-format) in my definition of the derived mode.
>
> Is there is way to do make `tabulated-list-format' variable? (that is, pass
> data to the macro define-derived-mode ?)
Of course.
Here is how similar mode is defined:
(define-derived-mode rcd-db-list-mode
tabulated-list-mode "About mode" "Description")
;; BTW, I need to highlight line in tabulatd list mode to avoid errors:
(add-hook 'tabulated-list-mode-hook #'hl-line-mode)
(defvar rcd-db-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map tabulated-list-mode-map)
(keymap-set map "g" #'goto-line)
(keymap-set map "u" #'rcd-tabulated-remove-marks)
(keymap-set map "v" #'rcd-tabulated-id-to-register)
(keymap-set map "\\" #'rcd-tabulated-filter-reset)
(keymap-set map "a" #'rcd-db-insert-new-row)
(keymap-set map "d" #'rcd-db-delete-tab-database-entry)
(keymap-set map "e" #'rcd-db-tabulated-edit-database-entry)
(keymap-set map "g" #'rcd-tabulated-refresh)
(keymap-set map "j" #'next-line)
(keymap-set map "k" #'previous-line)
(keymap-set map "m" #'rcd-tabulated-mark-id)
(keymap-set map "q" #'rcd-db-quit-window)
(keymap-set map "u" #'rcd-tabulated-unmark-id)
map)
"The basic RCD database keymap.")
Then I use generic function to report database entries, you need to adapt it,
watch variables beginning with tabulated-list-
(defun rcd-db-report (title entries format database-type db-handle table
sort-key &optional refresh highlight-list place id return-function)
"Main database report."
(setq rcd-db-current-database-type database-type)
(let* ((buffer (generate-new-buffer-name "My buffer")))
(let* ((buffer (get-buffer-create buffer))
(mode-map (rcd-db-table-mode-map table)))
(setq tabulated-list-format format)
(setq tabulated-list-entries entries)
(setq rcd-db-edited-table table)
(setq rcd-db-current-database-type database-type)
(setq rcd-tabulated-refresh-function refresh)
(setq rcd-current-return-function return-function)
(rcd-db-list-mode)
(use-local-map mode-map)
(setq rcd-db-current-database-handle db-handle)
(setq rcd-db-current-table (or (alist-get "table" place nil nil 'equal)
table))
(setq rcd-db-current-table-id id)
(setq tabulated-list-padding 1)
(tabulated-list-init-header))
(setq tabulated-list-sort-key sort-key)
(tabulated-list-print t)
(when highlight-list
(rcd-highlight-list highlight-list))))
You need variables:
(setq tabulated-list-format format)
(setq tabulated-list-entries entries)
(setq tabulated-list-padding 1) ;; not necessary
(tabulated-list-init-header)
(setq tabulated-list-sort-key sort-key)
(tabulated-list-print t)
in my case, depending of the table or other factor, the report function
automatically chooses key binding:
(defun rcd-db-table-mode-map (table)
"Generate mode map for table.
Use `rcd-db-current-database-type'."
(let ((symbol (format "rcd-db-%s-%s-mode-map" rcd-db-current-database-type
table)))
(if (intern-soft symbol)
(symbol-value (intern symbol))
rcd-db-mode-map)))
(rcd-db-report "Title" '((1 ["Something" ("Button" action (lambda (b) (message
"Hello")) font-lock-face link follow-link t)])) [("Column" 12 t) ("Links" 20
t)] "pg" nil "people" nil)
Functions in email will work, so if you evaluate the last one,
buffer will switch to tabulated list mode, it will be read only,
beware.
--
Jean
Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns
In support of Richard M. Stallman
https://stallmansupport.org/