help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is it alright to define-derived-mode dynamically?


From: Stefan Monnier
Subject: Re: Is it alright to define-derived-mode dynamically?
Date: Thu, 24 Dec 2020 09:44:52 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

> I wonder if it is alright to define the derived mode dynamically
> within a function:

For your own personal use, it may work just fine.
For an ELisp package that you expect other people to use, it'll bite
them sooner or later.

> (defun rcd-db-report (title entries &optional tabulated-list-format
>   tabulated-list-sort-key mode-map)
[...]
>       (define-derived-mode rcd-db-list tabulated-list-mode "Database List" 
> "Database List Report"
>         (hl-line-mode)
>         (use-local-map mode-map)
[...]
>       (rcd-db-list))))

First:
- It should be called `rcd-db-list-mode`.
- Don't use global variables's names for local variables.
- I doubt the mode needs `hl-line-mode`.  If *you* like it, then
  use something like (add-hook 'tabulated-list-mode-mode #'hl-line-mode) in
  your init file.

And you can do:

    (define-derived-mode rcd-db-list-mode tabulated-list-mode "Database List"
      "Major mode to manipulate Database List reports."
      [...])

    (defun rcd-db-report ( title entries &optional list-format
                           list-sort-key mode-map)
      [...]
      (rcd-db-list-mode)
      (use-local-map mode-map))

> I can see that I may define function within a function as well, but I
> wonder if that is alright.

`defun` manipulates the global environment (not just some environment
local to your `rcd-db-report`), and it throws away any previous
definition of the function, which is often not alright.


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]