all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* variable tabulated-list-format
@ 2022-10-16 22:42 John Haman
  2022-10-17  4:17 ` Jean Louis
  2022-10-17  4:24 ` Jean Louis
  0 siblings, 2 replies; 5+ messages in thread
From: John Haman @ 2022-10-16 22:42 UTC (permalink / raw)
  To: help-gnu-emacs

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 ?)

-John





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: variable tabulated-list-format
  2022-10-16 22:42 variable tabulated-list-format John Haman
@ 2022-10-17  4:17 ` Jean Louis
  2022-10-17 18:42   ` John Haman
  2022-10-18  1:01   ` John Haman
  2022-10-17  4:24 ` Jean Louis
  1 sibling, 2 replies; 5+ messages in thread
From: Jean Louis @ 2022-10-17  4:17 UTC (permalink / raw)
  To: John Haman; +Cc: help-gnu-emacs

* 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/



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: variable tabulated-list-format
  2022-10-16 22:42 variable tabulated-list-format John Haman
  2022-10-17  4:17 ` Jean Louis
@ 2022-10-17  4:24 ` Jean Louis
  1 sibling, 0 replies; 5+ messages in thread
From: Jean Louis @ 2022-10-17  4:24 UTC (permalink / raw)
  To: John Haman; +Cc: help-gnu-emacs

* John Haman <mail@johnhaman.org> [2022-10-17 01:43]:
> 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.

I spread SQL data into tabulated-list-mode:

(defun rcd-db-sql-report (title sql format table sort-key refresh &optional highlight-list return-function)
  "Prepare SQL for tabulated list report."
  (let* ((entries-1 (rcd-sql sql cf-db))
	 (entries-2
	  (mapcar #'(lambda (i)
		      (list
		       (elt i 0)
		       (vconcat
			(append (list
				 (cond ((numberp (elt i 0)) (number-to-string (elt i 0)))
				       (t (elt i 0))))
				(cdr (append i nil))))))
		  entries-1)))
    (cond (entries-1 (rcd-db-report title entries-2 format "pg" 'cf-db table sort-key refresh highlight-list nil nil return-function))
	  (t (rcd-warning-message (format "No entries for report `%s'" title))))))

Sample call looks as following:

(rcd-db-sql-report "Sample" "SELECT actives_id, actives_name FROM actives" [("ID" 5 t) ("Name" 10 t)] "actives" nil nil)

That generates following:

((2 ["2" "Inactive"]) (1 ["1" "Active"]))

I like to see the ID number, but it need not be shown. To do something
on the entry you use (tabulated-list-get-id) or
(tabulated-list-get-entry)

To generate data for tabulated-list-entries you have to use the
structure as above, each entry is list, first element is ID and other
element is vector.

Entries displayed in the current Tabulated List buffer.
This should be either a function, or a list.
If a list, each element has the form (ID [DESC1 ... DESCN]),
where:

 - ID is nil, or a Lisp object uniquely identifying this entry,
   which is used to keep the cursor on the "same" entry when
   rearranging the list.  Comparison is done with ‘equal’.

 - Each DESC is a column descriptor, one for each column
   specified in ‘tabulated-list-format’.  The descriptor DESC is
   one of:

    - A string, which is printed as-is, and must not contain any
      newlines.

    - An image descriptor (a list), which is used to insert an
      image (see Info node ‘(elisp) Image Descriptors’).

    - A list (LABEL . PROPS), which means to use
      ‘insert-text-button’ to insert a text button with label
      LABEL and button properties PROPS.  LABEL must not contain
      any newlines.

If ‘tabulated-list-entries’ is a function, it is called with no
arguments and must return a list of the above form.

  Automatically becomes permanently buffer-local when set.
  Probably introduced at or before Emacs version 29.1.


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: variable tabulated-list-format
  2022-10-17  4:17 ` Jean Louis
@ 2022-10-17 18:42   ` John Haman
  2022-10-18  1:01   ` John Haman
  1 sibling, 0 replies; 5+ messages in thread
From: John Haman @ 2022-10-17 18:42 UTC (permalink / raw)
  To: Jean Louis; +Cc: help-gnu-emacs

Thanks, this looks like a good example. When I get home, I will try to apply it to my code. I think I have all the right pieces, but maybe I am not evaluating them in the right order? I will report back if I can't get my function to correctly mimic yours.

-John




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: variable tabulated-list-format
  2022-10-17  4:17 ` Jean Louis
  2022-10-17 18:42   ` John Haman
@ 2022-10-18  1:01   ` John Haman
  1 sibling, 0 replies; 5+ messages in thread
From: John Haman @ 2022-10-18  1:01 UTC (permalink / raw)
  To: help-gnu-emacs

Thanks! I was able to get it working-ish.

-John



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-10-18  1:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-16 22:42 variable tabulated-list-format John Haman
2022-10-17  4:17 ` Jean Louis
2022-10-17 18:42   ` John Haman
2022-10-18  1:01   ` John Haman
2022-10-17  4:24 ` Jean Louis

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.