From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ergus Newsgroups: gmane.emacs.help Subject: Re: Simple macro question Date: Mon, 3 May 2021 13:32:56 +0200 Message-ID: <20210503113256.hjuw7jyhtpnxqnku@Ergus> References: <20210503000712.l2wh2liwhg3jfjxy.ref@Ergus> <20210503000712.l2wh2liwhg3jfjxy@Ergus> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="9825"; mail-complaints-to="usenet@ciao.gmane.io" Cc: help-gnu-emacs@gnu.org To: Stefan Monnier Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon May 03 13:37:12 2021 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 1ldWsy-0002Q5-MZ for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 03 May 2021 13:37:12 +0200 Original-Received: from localhost ([::1]:35990 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ldWsx-0002J8-KZ for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 03 May 2021 07:37:11 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:43316) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ldWpG-0006dH-5Y for help-gnu-emacs@gnu.org; Mon, 03 May 2021 07:33:22 -0400 Original-Received: from sonic310-13.consmr.mail.bf2.yahoo.com ([74.6.135.123]:42053) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ldWpC-0007v4-Ot for help-gnu-emacs@gnu.org; Mon, 03 May 2021 07:33:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aol.com; s=a2048; t=1620041596; bh=2kv3Cn0A36Lyfoi3uCiaDkHE3m4WOY/e1HTZerHU3xg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From:Subject:Reply-To; b=DuNwRzgFDEH5DV+K0e2OefUOMoA7FiPkNU65yltXDo9NSfYavnOAjTDcyKN/hAzjQnVhpqBK2LVe4A+Hznu7MDygwm4Di5S5QfBhFesB4ovM0PdAIv0gRSSGq0LkuqLX5fYpUugenwWcLVGHsxGrWHqwLhcpARaQno/OdLYqG4m3AqaJNizDUk+jHaxdPDhnQQ4OXo9J1M350TmOqAE1PAf7fsRWirVn1SZOdTsi91cUsDnsyUN6F2+OJ++RMT+uRbbGMzYhlh8OQfS326OREJkIi8rEFCsWLQ1VnwpAGpojWTeERkXTWHTRHWUI61TXHlM2NoI775ZtToLPh5/vUA== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1620041596; bh=GD83tNI/WlgCuP07eKD9SvK4T7oNXRVSwwtvPBu4w3d=; h=X-Sonic-MF:Date:From:To:Subject:From:Subject; b=khu/0io1pJbVoeV7SYAP2R6JoK9a8onJAN3LcMBAKxTqsCkTuc9SuIju2l757yEWiDQXNOb9r0JnmfecbJqFuYKG/uRHEUqZ5QLIb9jebN1XKN/KC+Sa/lp4yY8ZfCIlwHcG/eaZWzKCShUCdPKoTRDWPwbA9t7y5Jy4zfzHmSuMOnAT9heZjg+0MOMaQ6q6OMigAHpmscekQFNsKkydI2PRaNA3vp73U4tzNx0558DsRDSnhfk0Pb8N1DM6MbtZ36MLtdcE4489xb8uFNqXW7mBbglBgFJmZrRi00i9nOV+Xz+0bjNOLtq9VxEi0JOzIXh/lmY/Xw4xCHeaOVmICg== X-YMail-OSG: Lo6u9iUVM1koE9wthtVpZO.ujus8iy8GXDVru97RQVGIhXt3F.LM.tCs0FYlENL ozXDAzjVQW3lrY_QKQ8dtFdfpWzqz6BkEIwraF0vB4YDiQAalrXAZ37baMSHQfF32nxZ4zyMhUZS t4wWgKDJa_KJtgX69kK9clEn1LWmmWGd6MQVeIuB8GEIMGt7vPiVgRMl8O4m7GQwoBtXyKkmwPBx YaD7UrTYstHZuew24YdIqTA9cEn_zXcwPUNvQdOBItKCVOs_YFhxOy7b1yYNCMOEDJ2n7C6CnHfX aQchQgvXmRXDyHHxzIjBtkUH0MLjRMriScIieFasVK8_02U_zCugp3w5Zbv30QTi7FpuEmrwMNEO xPJKzAL59sOTZ9RUY1AREIXbNjLrCrM5t6kwgM03C3wPnBfBKcIiknUVi_KQMPZfDfrQUbumu98y wkmMkmQG6.IOScLiVq1_Aw.Lqp1zPnU2AtVpzEDPgFmxIfPKhy3K9sCoz00tXH1KAwfRtJMxUEC1 dDJplR0nszXdjggF64jR_p.KG1llDuT1c4iGZNr7Kjucdv3b9Ok2rpO0co0_tjb0ph0iBitS1BMo tLLPuvimnpGlGpvo1Ml7eX5moH8TMmLtxCipk8Q9GwgGPMSap.Y7Aj1KP_OgUjd7OVweYlCpkGHQ qsiCmoCei5mjor1uc8FAaFZtTMDmprSruIN3TTOdqVLensQpXUo1NGpI4RlyYPOxhi.PRV.IRFvB R1yevGWokQMnYYVh_yCPdNQW_HWt4F6CyWRffLuOD9M.vWj9_a6uuPFjVgadwhMMXoWXdEVNvLr2 hKsVZzakM.06k5D6nDmj_r8WnvMItKrtzClOf7nl3T X-Sonic-MF: Original-Received: from sonic.gate.mail.ne1.yahoo.com by sonic310.consmr.mail.bf2.yahoo.com with HTTP; Mon, 3 May 2021 11:33:16 +0000 Original-Received: by kubenode550.mail-prod1.omega.ir2.yahoo.com (VZM Hermes SMTP Server) with ESMTPA ID c0a138d9fea73284f14aafaba5b63ac3; Mon, 03 May 2021 11:33:12 +0000 (UTC) Content-Disposition: inline In-Reply-To: X-Mailer: WebService/1.1.18138 mail.backend.jedi.jws.acl:role.jedi.acl.token.atz.jws.hermes.aol Apache-HttpAsyncClient/4.1.4 (Java/16) Received-SPF: pass client-ip=74.6.135.123; envelope-from=spacibba@aol.com; helo=sonic310-13.consmr.mail.bf2.yahoo.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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:129370 Archived-At: Hi Stefan: Thanks for the reply. IIUC then this code: ==================== ;;; test.el --- test editing -*- lexical-binding: t; -*- ;;; Commentary: ;;; Code: (defun test-fun (com) "Testfun with COM." nil) (defmacro test-def (thelist) "Doc def." `(progn ,@(mapcar #'test-fun thelist))) (test-def (A B C)) (provide 'test) ;;; test ends here ============================ Will require to put the test-fun inside an `eval-and-compile` too? Because I am getting a similar error, this time test-fun is not defined. I tried to use eval-and-compile in this case but the problem is that in my real code test-fun calls another function (a minor mode) that needs to be defined later (in the same file); and I get an error in that case too that the minor-mode-function is not defined. What's the canonical way to do that? use declare function? some trick with the quoting? Put also the minor mode inside an eval-and-compile? In case you are interested into why I came into this issues, here is the code: https://github.com/Ergus/composable.el/blob/master/composable.el In the future I want to implement another version using the new `repeat-mode` api, that will be cleaner, but at the moment it does not makes sense as there is not any emacs release with the repeat api yet. Thanks in advance, Ergus On Sun, May 02, 2021 at 09:46:30PM -0400, Stefan Monnier wrote: >> (defvar test-commands-list '(A B C) >> "List of replaced functions.") >> (defmacro test-def () >> "Doc def." >> `(eval-and-compile ,@(mapcar (lambda (com) nil) test-commands-list))) >> (test-def) > >[...] > >> Symbol’s value as variable is void: test-commands-list > >macro-expansion takes place during compilation. During compilation, the >code is mostly translated, not evaluated, but macros are expanded. >So your `(test-def)` macro call is expanded which evaluates the code >inside `test-def` whereas the `(defvar ...)` code is not evaluated (it's >only translated into byte-code) and hence the var is not defined: it >will only be defined much later when the resulting byte-code is run. > >One way to work around it is to place the `defvar` within an >`eval-and-compile`. > >> (defmacro test-def (commands-list) >> ... >> >> (test-def test-commands-list) >> >> In this second case I get: wrong arguments sequencep test-commands-list >> Is this intended? > >Yes: the argument to macros are the actual sexp written in the macro >call, not the result of their evaluation. So your argument >`commands-list` will hold the symbol `test-commands-list` rather than >the list you were hoping to get. You could use `symbol-value` to get >the content of that symbol as a variable, but then you'd be back to the >previous problem because the `defvar` has not been evaluated yet. > > > Stefan > >