From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.help Subject: Re: Is it alright to define-derived-mode dynamically? Date: Thu, 24 Dec 2020 09:44:52 -0500 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19995"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) To: help-gnu-emacs@gnu.org Cancel-Lock: sha1:UjDBvUt884rUwy6ewpaq+opmyLU= Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Dec 24 15:45:22 2020 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ksRrk-00054Z-N0 for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 24 Dec 2020 15:45:20 +0100 Original-Received: from localhost ([::1]:53330 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ksRrj-0006DL-Pl for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 24 Dec 2020 09:45:19 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:46066) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ksRrR-0006Cn-0v for help-gnu-emacs@gnu.org; Thu, 24 Dec 2020 09:45:01 -0500 Original-Received: from ciao.gmane.io ([116.202.254.214]:44180) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ksRrP-0001DQ-IG for help-gnu-emacs@gnu.org; Thu, 24 Dec 2020 09:45:00 -0500 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1ksRrN-0004fA-3E for help-gnu-emacs@gnu.org; Thu, 24 Dec 2020 15:44:57 +0100 X-Injected-Via-Gmane: http://gmane.org/ Received-SPF: pass client-ip=116.202.254.214; envelope-from=geh-help-gnu-emacs@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:126746 Archived-At: > 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