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: Eval in templates - Re: How to tame compiler? Date: Mon, 3 May 2021 09:51:35 +0300 Message-ID: References: <875z03th9b.fsf@web.de> <87v981wv3f.fsf@web.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="4449"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/2.0.6 (2021-03-06) Cc: help-gnu-emacs To: Eduardo Ochs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon May 03 08:54:44 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 1ldSTb-00013P-Vv for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 03 May 2021 08:54:44 +0200 Original-Received: from localhost ([::1]:43156 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ldSTb-0001GF-0i for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 03 May 2021 02:54:43 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:40936) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ldSTH-0001G6-4D for help-gnu-emacs@gnu.org; Mon, 03 May 2021 02:54:23 -0400 Original-Received: from stw1.rcdrun.com ([217.170.207.13]:48243) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ldSTE-0004MU-Iw for help-gnu-emacs@gnu.org; Mon, 03 May 2021 02:54:22 -0400 Original-Received: from localhost ([::ffff:154.226.104.226]) (AUTH: PLAIN securesender, TLS: TLS1.3,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by stw1.rcdrun.com with ESMTPSA id 00000000000462BB.00000000608F9DFA.00007C58; Sun, 02 May 2021 23:53:46 -0700 Mail-Followup-To: Eduardo Ochs , help-gnu-emacs Content-Disposition: inline In-Reply-To: Received-SPF: pass client-ip=217.170.207.13; envelope-from=bugs@gnu.support; helo=stw1.rcdrun.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-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:129366 Archived-At: * Eduardo Ochs [2021-05-03 04:59]: > just as a curiosity, here is how eev implements template > interpolation: > > http://angg.twu.net/eev-current/eev-template0.el.html > http://angg.twu.net/eev-current/eev-template0.el > > I tried several other implementations of template interpolation > functions before that one, and from time to time I would find another > way that would be both simpler and more powerful than the previous > one... the one above is so simple that in one of the times that I gave > a minicourse on LaTeX and Emacs I followed the code of `ee-template0' > with the students in the third day of the minicourse - one day after > teaching them briefly how to understand code with `defun's and > `let's. That is so interesting. This templating system has slightly different concept. In my eval-ing, I have to get some value no matter what, I am also thinking I have freedom to make errors, to the system is not allowed to fail. (setq rcd-template-delimiter-open "(") ⇒ "(" (setq rcd-template-delimiter-close ")") ⇒ ")" (setq text1 "We will evaluate (+ 2 2) to result ((+ 2 2))") ;; won't interpolate (setq text2 "We will evaluate (+ 2 2) to result ( (+ 2 2) )") (setq text3 "We will evaluate (+ 2 2) to result ( (+ 2 2 )") ;; error allowed This does not interpolate, as I demand spaces after delimiters: (rcd-template-eval text1) ⇒ "We will evaluate (+ 2 2) to result ((+ 2 2))" This does interpolate: (rcd-template-eval text2) ⇒ "We will evaluate (+ 2 2) to result 4" Error is interpolated to "", but I could log it in future: (rcd-template-eval text3) ⇒ "We will evaluate (+ 2 2) to result " Compared to this that works: (ee-template0 "{<} a{(+ 2 3)} {>}") ⇒ "{ a5 }" And this one giving error: (ee-template0 "{<} a{(+ 2 3} {>}") Concept is the same. You also do not use dynamic binding, which is quite clear why. In the sense of teaching is fine. I guess you almost never had a problem when evaluation fails for some reason. In the sense of generation of hundreds of emails for people, or generation of thousands of HTML pages, the template system IMHO is not allowed to fail. Errors however, could be reported, templating system could then tell me in which page or email the error has been found. But it has to move silently over the failure. Thus I would expect also this one, to produce following: (ee-template0 "{<} a{(+ 2 3} {>}") ⇒ "{ a }" And more perfect would be if error would be reported somewhere in silent. > My trick to handle lexical binding was to tell the students that > dynamic binding is much easier to understand than lexical binding, and > that they should treat dynamic binding as an advanced topic that would > make more sense when they had at least one week of experience with > Emacs - and my minicourse was only five days long, from monday to > friday... but the students in that minicourse were mathematicians with > little experience in programming, not compsci people who would see > immediately that dynamic binding is "wrong". Thus you had the luck to have a different type of audience. Here is also demonstrated how eev tools have different context, programs are invoked from within text manuall. Your tools are in general, beyond templating, meant to be evaluated in the text or within the text and in that concept there is also no need for lexical binding. Small snippets are evaluated one after each other by user's demand, functions evaluated may come originally from programs with lexical binding. Thus I have improved my function to evalute with errors reported, as it makes so much more sense. It is made to interpolate thousands of times within minutes automatically. Source: https://gnu.support/files/sources/rcd-template.el (defun rcd-template-eval (string &optional delimiters) "Evaluates Emacs Lisp enclosed by `rcd-template-delimiter-open' and `rcd-template-delimiter-close'. Optional DELIMITERS list may be provided to change default delimiters, first list member has to be the opening delimiter and second the closing delimiter. Space or new line has to follow `rcd-template-delimiter-open' and precede `rcd-template-delimiter-close' for evaluation to get invoked." (let* ((delimiters (or delimiters (list rcd-template-delimiter-open rcd-template-delimiter-close))) (open (car delimiters)) (close (cadr delimiters)) (add (length open)) (minus (length close))) (with-temp-buffer (insert string) (goto-char 0) (while (re-search-forward (rx (literal open) (one-or-more (or blank "\n")) (group (minimal-match (one-or-more anything))) (one-or-more (or blank "\n")) (literal close)) nil t) (let* ((match-beginning (match-beginning 0)) (match-end (match-end 0)) (match (buffer-substring-no-properties (+ add match-beginning) (- match-end minus))) (lisp (condition-case error (read-from-string match) (error (princ (format "rcd-template-eval: `read-from-string' error: %s" error)) ""))) (lisp (if (listp lisp) (car lisp) lisp)) (value (condition-case error (eval lisp) (error (princ (format "rcd-template-eval: `eval' error: %s" error))))) (value (cond ((null value) "") (t (format "%s" value))))) (delete-region match-beginning match-end) (insert (format "%s" value)))) (buffer-string)))) (rcd-template-eval "( (+ 2 2) )" '("(" ")")) ⇒ "4" (rcd-template-eval "( (+ 2 2 )" '("(" ")")) ⇒ "" with error reporting in *Messages* buffer: rcd-template-eval: `read-from-string' error: (end-of-file) (rcd-template-eval "We expect 4 but fail: ( (+2 2) )" '("(" ")")) ⇒ "We expect 4 but fail: " with error reporting in *Messages* buffer: rcd-template-eval: `eval' error: (invalid-function 2) (defun ee-template-test (text) (rcd-template-eval text '("{" "}"))) My regexp asks for spaces or new lines between delimiters: (ee-template-test "{<} a{ (+ 2 3 } {>}") ⇒ "{<} a {>}" with error reporting in *Messages* buffer: rcd-template-eval: `read-from-string' error: (end-of-file) but of course, it does not do your trick: (ee-template-test "{<} a{ (+ 2 3) } {>}") ⇒ "{<} a5 {>}" If I would like to enclose it with open delimiters in a result, this would work: (ee-template-test "{ \"{\" } a{ (+ 2 3) } { \"}\" }") ⇒ "{ a5 }" And I use the habit that people would not tend to write space after parenthesis. I could write: (maybe I am wrong), but I would never write ( maybe I am not wrong ). That may allow using parenthesis in templates which makes it more Lisp-like: (rcd-template-eval "We know that (+ 2 2) evaluates to ( (+ 2 2) )") ⇒ "We know that (+ 2 2) evaluates to 4" Then `eval' within `eval' help in avoiding repetition of demonstrated code: (rcd-template-eval "We know that ( (setq code '(+ 2 2)) ) evaluates to ( (eval code) )") ⇒ "We know that (+ 2 2) evaluates to 4" Regular expression is I see hard coded: (defvar ee-template00-re "{\\([^{}]+\\)}") And I have envisioned it to rather have flexible delimiters: (rcd-template-eval "<% user-login-name %>" '("<%" "%>")) ⇒ "jimmy" as it emulates some other templating engines and allows re-use of templates: (rcd-template-eval "<% @var user-login-name %>" '("<% @var" "%>")) ⇒ "jimmy" Maybe you wish to include error reporting and silent failure as well, or no? -- Jean Take action in Free Software Foundation campaigns: https://www.fsf.org/campaigns Sign an open letter in support of Richard M. Stallman https://stallmansupport.org/ https://rms-support-letter.github.io/