From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Chunyang Xu Newsgroups: gmane.emacs.help Subject: Re: Can el-search-query-replace replace mapcar with --map ? Date: Sat, 23 Dec 2017 14:17:25 +0800 Message-ID: References: <87a7yahebe.fsf@web.de> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1514009790 31054 195.159.176.226 (23 Dec 2017 06:16:30 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 23 Dec 2017 06:16:30 +0000 (UTC) User-Agent: mu4e 0.9.18; emacs 27.0.50 Cc: help-gnu-emacs@gnu.org To: Michael Heerdegen Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Dec 23 07:16:26 2017 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eSd6f-0007gR-Jo for geh-help-gnu-emacs@m.gmane.org; Sat, 23 Dec 2017 07:16:25 +0100 Original-Received: from localhost ([::1]:40368 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eSd8d-0004Yl-Uo for geh-help-gnu-emacs@m.gmane.org; Sat, 23 Dec 2017 01:18:27 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60882) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eSd8A-0004YU-VS for help-gnu-emacs@gnu.org; Sat, 23 Dec 2017 01:17:59 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eSd86-0005TL-W6 for help-gnu-emacs@gnu.org; Sat, 23 Dec 2017 01:17:58 -0500 Original-Received: from forward103p.mail.yandex.net ([77.88.28.106]:38261) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eSd86-0005SN-J5 for help-gnu-emacs@gnu.org; Sat, 23 Dec 2017 01:17:54 -0500 Original-Received: from mxback20j.mail.yandex.net (mxback20j.mail.yandex.net [IPv6:2a02:6b8:0:1619::114]) by forward103p.mail.yandex.net (Yandex) with ESMTP id 0E6922183178; Sat, 23 Dec 2017 09:17:52 +0300 (MSK) Original-Received: from smtp1p.mail.yandex.net (smtp1p.mail.yandex.net [2a02:6b8:0:1472:2741:0:8b6:6]) by mxback20j.mail.yandex.net (nwsmtp/Yandex) with ESMTP id ZnVBDRf73M-HpPWmlqe; Sat, 23 Dec 2017 09:17:52 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=xuchunyang.me; s=mail; t=1514009872; bh=XMcJetcMA3QtlLEGkTrJINX7Jzy09PQ8zHRyItgeX0A=; h=References:From:To:Cc:Subject:In-reply-to:Date:Message-ID; b=zDXG6fUU3rD56q2VZ9mMR4MVlVhrnj/sEv4KIloKoMAMNP5cXCbrjvhBIyOySwGnu 03cmARZIBRPkli5U8Mu37H9ZXruZs5EiN6MMuebDxuVzafN2miN664+u8o+tTjeJdv H/3nhhQxX0k6QuL0piRiBVsJxPBlGmtkc+7SpCXk= Original-Received: by smtp1p.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id MvnNCd1SRD-He0K1CkP; Sat, 23 Dec 2017 09:17:49 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client certificate not present) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=xuchunyang.me; s=mail; t=1514009870; bh=XMcJetcMA3QtlLEGkTrJINX7Jzy09PQ8zHRyItgeX0A=; h=References:From:To:Cc:Subject:In-reply-to:Date:Message-ID; b=gVg94ROlbdQEPzsydOBchUX3fxVTSraSMHsETCxcnOe8cjoePT3wZYRCHhBu9OgIZ j6Pyy8/1EQqRluziRRxuN9TzymExK03rWYq93cvCYCv1T7cqEJS5vmlFHAkTDabmxg DJ5HVuwlAINU2S+K4GGjWw4i9V0rTGR4KsGBS20g= Authentication-Results: smtp1p.mail.yandex.net; dkim=pass header.i=@xuchunyang.me In-reply-to: <87a7yahebe.fsf@web.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 77.88.28.106 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 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.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:115435 Archived-At: Michael Heerdegen writes: > Chunyang Xu writes: > >> It gives me: >> >> (--map >> (* x x) >> '(1 2 3)) > > which is expected, of course. > > The transformation you want is not trivial, and there are some > problematic cases when it gets complicated - e.g. when the mapped lambda > uses in its body a local variable with the same name as the argument > variable, like in > > #+begin_src emacs-lisp > (lambda (x) (let ((x (1+ x))) (* x x))) > #+end_src > > But that should be corner cases in practice. > > I would define a helper function to perform the renaming of the lambda > argument into "it" - something like > > #+begin_src emacs-lisp > (defun transform-lambda-form-for---map (lambda-form) > (pcase-let ((`(lambda (,var) . ,body) lambda-form)) > (macroexpand-1 > `(cl-symbol-macrolet ((,var it)) > ,@body)))) > #+end_src > > and then el-search-query-replace with the rule > > #+begin_src emacs-lisp > `(mapcar ,lambda-form ,list) > -> > `(--map ,(transform-lambda-form-for---map lambda-form) ,list) > #+end_src > > Does that work ok for you? It works fine for my example: (mapcar (lambda (x) (* x x)) '(1 2 3)) but not (mapcar (lambda (x) (let ((x (1+ x))) (* x x))) '(1 2 3)) with the exact same patterns, 'el-search-query-replace' reports el-search--format-replacement: Error in =E2=80=98el-search--format-replac= ement=E2=80=99 - please make a bug report I guess this is excepted by you, even though 'transform-lambda-form-for---map' is correct for both cases: (transform-lambda-form-for---map (lambda (x) (* x x))) =3D> (* it it) (transform-lambda-form-for---map (lambda (x) (let ((x (1+ x))) (* x x)))) =3D> (let ((x (1+ it))) (* x x)) By the way, is there a way to tidy the code after 'el-search-query-replace' ? For the same example above, it produces (--map (* it it) '(1 2 3)) but I want (--map (* it it) '(1 2 3)) right now I have to do it manually via M-^ ('delete-indentation'), which is not very convenient. Thanks for your help.