From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Madhu Newsgroups: gmane.emacs.help Subject: Re: the (declare special) declaration with lexical scope. Date: Mon, 24 Apr 2023 07:46:01 +0530 (IST) Message-ID: <20230424.074601.1016279240196114273.enometh@meer.net> References: Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="20275"; mail-complaints-to="usenet@ciao.gmane.io" Cc: help-gnu-emacs@gnu.org To: platon7pronko@gmail.com Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Apr 24 04:18:17 2023 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 1pqlmT-0005A2-S1 for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 24 Apr 2023 04:18:17 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pqllt-0006rP-C3; Sun, 23 Apr 2023 22:17:41 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pqllr-0006rF-A9 for help-gnu-emacs@gnu.org; Sun, 23 Apr 2023 22:17:39 -0400 Original-Received: from smtp5.ctinetworks.com ([205.166.61.198]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pqllp-0007Tc-Pk for help-gnu-emacs@gnu.org; Sun, 23 Apr 2023 22:17:39 -0400 Original-Received: from localhost (unknown [117.193.0.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: enometh@meer.net) by smtp5.ctinetworks.com (Postfix) with ESMTPSA id 2527B165D14; Sun, 23 Apr 2023 22:16:27 -0400 (EDT) In-Reply-To: X-Mailer: Mew version 6.9 on Emacs 30.0.50 X-ctinetworks-Information: Please contact the ISP for more information X-ctinetworks-MailScanner-ID: 2527B165D14.A358A X-ctinetworks-VirusCheck: Found to be clean X-ctinetworks-Watermark: 1683166590.5607@vqQTC8QEa7jc7qfP+UCv3w Received-SPF: pass client-ip=205.166.61.198; envelope-from=enometh@meer.net; helo=smtp5.ctinetworks.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_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 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-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:143357 Archived-At: * Platon Pronko Wrote on Sun, 23 Apr 2023 17:47:40 +0800 > However I'm interested why you didn't consider to make the entire > mustache function into a macro that expands to concat call with > variables already interpolated? This way it will work regardless of > lexical or dynamic binding. You're right. If I had started that way it lexenv wouldn't have been an issue and my goal here was mainly to get the "rendering context" from the (dynamic or lexical) environment. But I imagined the idea of "templating" involves compiling a template pattern to a funcallable and calling it repeatedly with different arguments. > I suppose this might break if template is dynamically generated > somewhere, however templates are usually static and thus can be > embedded at compile time (it will be the fastest-performing option, > aside from other benefits). > > Here's an example of what I mean: > > (defun mustache-build (template) > (let ((pattern-start (string-match "{{\\(.*?\\)}}" template))) > (if (not pattern-start) > template > `(,(substring template 0 pattern-start) > (prin1-to-string ,(intern (match-string 1 template))) > ,@(if (>= (match-end 0) (length template)) nil (mustache-build > (substring template (match-end 0)))))))) > (defmacro mustache (template) > `(concat ,@(mustache-build template))) > > (macroexpand '(mustache "a={{a}} b={{b}}")) > ; (concat "a=" (prin1-to-string a) " b=" (prin1-to-string b)) > > (let ((a 42) (b '(+ 2 3))) > (mustache "a={{a}} b={{b}}")) > ; "a=42 b=(+ 2 3)" This is enough for the problem I was working on.