From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jean Louis Newsgroups: gmane.emacs.help Subject: Re: [SOLVED with `eval']: Why I cannot use this variable in macro call from function? Date: Wed, 9 Jun 2021 17:39:25 +0300 Message-ID: References: <20210609060959.GA21706@tuxteam.de> <20210609065129.GB21706@tuxteam.de> <20210609073928.GC21706@tuxteam.de> <20210609085406.GH21706@tuxteam.de> <20210609113353.GB4155@tuxteam.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="30346"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/2.0.7+183 (3d24855) (2021-05-28) Cc: Help GNU Emacs To: tomas@tuxteam.de Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Jun 09 16:41:39 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 1lqzOk-0007jd-VM for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 09 Jun 2021 16:41:39 +0200 Original-Received: from localhost ([::1]:34506 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lqzOj-0000vU-Pg for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 09 Jun 2021 10:41:37 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:43522) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lqzOP-0000tR-4k for help-gnu-emacs@gnu.org; Wed, 09 Jun 2021 10:41:17 -0400 Original-Received: from stw1.rcdrun.com ([217.170.207.13]:54105) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lqzON-0002F2-7o for help-gnu-emacs@gnu.org; Wed, 09 Jun 2021 10:41:16 -0400 Original-Received: from localhost ([::ffff:197.157.0.23]) (AUTH: PLAIN admin, TLS: TLS1.3,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by stw1.rcdrun.com with ESMTPSA id 00000000000ADF27.0000000060C0D307.00000252; Wed, 09 Jun 2021 07:41:10 -0700 Mail-Followup-To: tomas@tuxteam.de, Help GNU Emacs Content-Disposition: inline In-Reply-To: <20210609113353.GB4155@tuxteam.de> Received-SPF: pass client-ip=217.170.207.13; envelope-from=bugs@gnu.support; helo=stw1.rcdrun.com X-Spam_score_int: -3 X-Spam_score: -0.4 X-Spam_bar: / X-Spam_report: (-0.4 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_SORBS_WEB=1.5, SPF_HELO_PASS=-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:130674 Archived-At: ;; -*- lexical-binding: t; -*- * tomas@tuxteam.de [2021-06-09 14:35]: > On Wed, Jun 09, 2021 at 01:56:45PM +0300, Jean Louis wrote: > > * tomas@tuxteam.de [2021-06-09 11:54]: > > > On Wed, Jun 09, 2021 at 11:22:38AM +0300, Jean Louis wrote: > > > > * tomas@tuxteam.de [2021-06-09 10:40]: > > > > > You snipped the (for me) interesting part: did you notice how > > > > > `eval' jumps over the local declaration? > > > > > > > > Do you mean variables within `let'? > > > > > > Yes, it doesn't see them :) > > > > Maybe in theory it does not see, but in reality it does see it as > > `list' is evaluated before `eval', so the interned `rcd-symbol' and > > variable `description' they get evaluated before `eval'. > That sentence doesn't make any sense to me. It does or it doesn't. Well, I get confused too, you said that it does not see, but it is obvious that it does see. > I propose to you the next experiment: > > * experiment 3 > > (let () > (let ((x 42)) > (eval '(progn (setq x 43) (message "in eval: x is %S" x))) > (message "inner let: x is %S" x)) > (message "outer let: x is %S" x)) > > (you might have to switch to the *Messages* buffer to see all three > messages). The outer scope does not see the inner scope. Then the buffer *Backtrace* jumps up: Debugger entered--Lisp error: (void-variable x) (message "outer let: x is %S" x) (let nil (let ((x 42)) (eval '(progn (setq x 43) (message "in eval: x is %S" x))) (message "inner let: x is %S" x)) (message "outer let: x is %S" x)) eval((let nil (let ((x 42)) (eval '(progn (setq x 43) (message "in eval: x is %S" x))) (message "inner let: x is %S" x)) (message "outer let: x is %S" x)) nil) elisp--eval-last-sexp(nil) eval-last-sexp(nil) funcall-interactively(eval-last-sexp nil) call-interactively(eval-last-sexp nil nil) command-execute(eval-last-sexp) > What are the results? Do they correspond to your expectations? If > not, why not? I did not have any expectations for that piece of code and that one does not generate new global variables to return the symbol for history, which is what I need, and what is solved with `eval' nicely. Look here: (let ((x 42)) (eval (list 'progn (setq x 43) (message "in eval: x is %S" x)))) ⇒ "in eval: x is 43" that obviously does work nicely as the list gets evaluated before eval receives it. At least I assume it is so, according to learning about the LISP in general. In your example you have given pure data to `eval' in which case the variables within data don't get expanded. There is other way to expand them but using `list': (let ((x 43)) (eval `(+ 2 ,x))) ⇒ 45 It seems that this works too: (let ((x 43)) (eval '(+ 3 x))) ⇒ 46 While this will not work: (let () (let ((x 43)) (eval '(+ 3 x))) x) but this will also not work and is not related to `eval': (let () (let ((x 43)) x) x) As how I see that code above from you it is by its meaning similar to that one above. By the way, `eval' is powerful LISP function in general, it obviously has its uses when nothing else works. Example here below is when Emacs Lisp is read from a database. User can define a type of a page and how to process the page, for example with (rcd-markdown wrs-text) which symbolic expression is fetched from the database. Of course, by allowing user to define any Emacs Lisp into database there are security issues and those are solved with permissions. rcd-cf.el:6444: (eval (car (read-from-string (rcd-db-get-entry "pagetypes" "pagetypes_elisp" type cf-db)))))) For some small functions I use `eval': rcd-devel-utilities.el:57: (let ((result (eval (elisp--preceding-sexp)))) (defun replace-last-sexp () "Eval last sexp and replaces it in the buffer with its result." (interactive) (let ((result (eval (elisp--preceding-sexp)))) (kill-sexp -1) (insert (format "%s" result)))) I can find I have been using it in the package `rcd-hash-edit.el' very handy to edit visually a hash and dispatch the hash by email, or other communication to collaborator: (defun rcd-hash-create (symbol) "Create hash NAME. Argument SYMBOL will be created in global space." (eval `(defvar ,(intern (symbol-name symbol)) (make-hash-table :test 'equal)))) I heard much talk of `eval' being bad, but that is not true inherently. function is powerful and one can feed data to function and get it evaluated. When public is supplying input to `eval' then there are some safety issues, but nothing so much more than without eval. People accept input without `eval' and they forget quoting or parsing and people enter into web servers or intrude into systems. Let's not forget, this is LISP, it is for `eval'-ing. Yet, nothing from the above is not helping to make dynamically generated global variables, do you know how to solve it in the second run without `eval'? -- Jean Take action in Free Software Foundation campaigns: https://www.fsf.org/campaigns In support of Richard M. Stallman https://stallmansupport.org/