From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jon Eskin Newsgroups: gmane.emacs.devel Subject: Re: [Patch] Add project.el command to replace symbol at point throughout project Date: Sat, 15 Jan 2022 04:55:52 -0500 Message-ID: <0617E8BF-CC43-4D82-8648-064252F644FD@gmail.com> References: <83zgo2770l.fsf@gnu.org> <83zgo15dhp.fsf@gnu.org> <83czkw3uvl.fsf@gnu.org> <170a1c89-7767-0930-f726-f7b2551876b2@yandex.ru> <44f72f97-d1d8-0b4c-73a0-7f077fb1ef26@yandex.ru> Mime-Version: 1.0 (Mac OS X Mail 15.0 \(3693.40.0.1.81\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_2F99CCF6-1B02-4126-B774-97AE9DABC3A2" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="17018"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , emacs-devel@gnu.org To: Dmitry Gutov , Juri Linkov Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Jan 15 10:57:25 2022 Return-path: Envelope-to: ged-emacs-devel@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 1n8foK-0004F0-NN for ged-emacs-devel@m.gmane-mx.org; Sat, 15 Jan 2022 10:57:24 +0100 Original-Received: from localhost ([::1]:32930 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n8foJ-0000zV-Bd for ged-emacs-devel@m.gmane-mx.org; Sat, 15 Jan 2022 04:57:23 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:50624) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n8fmu-0005M2-Pn for emacs-devel@gnu.org; Sat, 15 Jan 2022 04:55:57 -0500 Original-Received: from [2607:f8b0:4864:20::f2b] (port=33433 helo=mail-qv1-xf2b.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n8fmt-0002mm-3R; Sat, 15 Jan 2022 04:55:56 -0500 Original-Received: by mail-qv1-xf2b.google.com with SMTP id t7so12638322qvj.0; Sat, 15 Jan 2022 01:55:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=7NvP2kyZNUxTTaiskPwukrzrORSLFkzQ2JjAKCTmXfs=; b=qoVR2MYuM+L7T2cDGWSQBMnG2VyWwu4/EZSMO5eekYH60PV1Gn7KtXwu6FKDUW94ZO 2iK1z5Mya+P4VqzVt9NwHImIxCm6qMkD725B/Nl5fgdkhRqhOhqyTW2SlHOz3Xp5QZ5n ZFboH9dfrx0mVZ2Mt1BZGYZ8aVUkgaPdK6NVl7KA8msHW0QzeYFIa9sHDJYnDWplfUy/ gITN9sClRDqMrTQdbOOyPMC40YrXyQEPpMS2UoA05pTdKkgVDY13WaeOJGKLTYdXA1ll TXepREbqfUcuDx8xXfCOpxRR5w3iVe2BJ2gGciQoqeDwFNw+6WFhUC6yL307fZ9b4+6Z 71XQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=7NvP2kyZNUxTTaiskPwukrzrORSLFkzQ2JjAKCTmXfs=; b=6VjAFs/EE7bCPDeLAnorM4Za37PKqXDpV0wCvBNiKBmcSgUiHbQY+Os98C9WWqJyk6 chRL/ImN7oYGrZTBqJshueM4jCht0/7Ky7dNtAIwjf3kC5vE4R2hnSIdlWf4yu49HrU4 tRjR6kHxYEgXxcFnKfxH6vktSzsUfHlqc7ip5JK4XvapDkSAECU9Pz+9zQK8DRpfRXdk 2wMDaUwCw8IoIkAkeWN6YwkXX3Sf/qq8RFCUMBXWTwUpRIQgaD1RELgNn96XCnwqlTOI kehesY0g8riK9DQAaHnVpRXKA2zqYhcVd01RAAQU0SbAfzeSY0QPxAJxnVF3AkbadAQi jFsA== X-Gm-Message-State: AOAM530gPrhvM4v/1SXtehpD8AHmaL9ksBI+1Nmfp2V8ZxvlQpuRbUlt CPy/kP/UML91KHmjAMBieew= X-Google-Smtp-Source: ABdhPJxae0kl1HdOI9Xqr0wDLMg5Jh+kJvIjyL7ttTnvt94RfGUNHso7N/vMU2DKkacQYTQbg0xenQ== X-Received: by 2002:a05:6214:2aaa:: with SMTP id js10mr7556237qvb.115.1642240553393; Sat, 15 Jan 2022 01:55:53 -0800 (PST) Original-Received: from smtpclient.apple (pool-108-31-85-19.washdc.fios.verizon.net. [108.31.85.19]) by smtp.gmail.com with ESMTPSA id j22sm4939344qko.117.2022.01.15.01.55.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 15 Jan 2022 01:55:52 -0800 (PST) In-Reply-To: <44f72f97-d1d8-0b4c-73a0-7f077fb1ef26@yandex.ru> X-Mailer: Apple Mail (2.3693.40.0.1.81) X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::f2b (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::f2b; envelope-from=eskinjp@gmail.com; helo=mail-qv1-xf2b.google.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 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, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:284770 Archived-At: --Apple-Mail=_2F99CCF6-1B02-4126-B774-97AE9DABC3A2 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > On Jan 14, 2022, at 3:28 PM, Dmitry Gutov wrote: >=20 > On 14.01.2022 12:26, Jon Eskin wrote: >>> So Jon, if you fancy writing a patch in this area, you can try = implementing what Juri suggested: >>>=20 >>> let-bind read-regexp-defaults-function around the call >>> of query-replace-read-args, and use a symbol at point as the = default. >>>=20 >>> That can apply to both project-query-replace-regexp and = xref-find-references-and-replace. >> On it! I might take a little while but I'll check in if I get stuck. >=20 > Yeah, don't hesitate to ask questions if/when you have any. Hey all, I took a stab at the implementation Juri described for = `project-query-replace-regexp` and I was able to get the correct = behavior, but I wanted to check in and see if it needs improvements = before looking at `xref-find-references-and-replace.` When I first let-bound `read-regexp-defaults-function` around the call = to `query-replace-read-args`, it didn't work- it looks like = `read-regexp` needs to be passed a symbol for its `DEFAULTS` parameter = or it ignores `read-regexp-default-function`. I passed in the symbol at = point to `DEFAULTS` which works- if I understand correctly the value of = any I pass in doesn't end up making a difference as long as it's a = symbol. I wasn't sure what the reason was for that behavior, but I = didn't want to mess with stuff I didn't understand. Another issue is that hardcoded logic in `read-regexp`: 'If PROMPT ends in \":\" (followed by optional whitespace), use it as-is. Otherwise, add \": \" to the end, possibly preceded by the default result (see below).' The PROMPT passed into read-regexp does end in a ":" due to it being = formatted by a call to `format-prompt` in `query-replace-read-from`. As = a result, when the symbol is at point, `read-regexp` display the prompt = with the last replacement from history. To address this, I added a cond = case where the formatting takes place and omit the formatting step when = there is a symbol at point so that `read-regex` will correctly format = the prompt with the symbol at point.=20 The result is that a call to `project-query-replace-regexp` will take = the symbol at point as the default when available, otherwise it will use = its previous behavior of defaulting to the last replacement available in = history. Let me know what you think. --Apple-Mail=_2F99CCF6-1B02-4126-B774-97AE9DABC3A2 Content-Disposition: attachment; filename=0001-Use-symbol-at-point-as-default-for-query-replace-rea.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="0001-Use-symbol-at-point-as-default-for-query-replace-rea.patch" Content-Transfer-Encoding: quoted-printable =46rom=2086c180b8b9021eb96ac2ea2f75682753a3822e50=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20Jon=20Eskin=20=0ADate:=20= Fri,=2014=20Jan=202022=2010:03:30=20-0500=0ASubject:=20[PATCH]=20Use=20= symbol=20at=20point=20as=20default=20for=20query-replace-read-args=0A=0A= *=20lisp/progmodes/project.el=0A(project-query-replace-regexp):=20= Let-bind=0A`read-regexp-defaults-function`=20to=20default=20to=20symbol=20= at=20point.=0A=0A*=20lisp/replace.el=0A(query-replace-read-from):=20= Don't=20format=20prompt=20if=20there's=20a=20symbol=20at=0Apoint,=20= otherwise=20read-regexp=20is=20forced=20to=20use=20the=20wrong=20prompt.=0A= ---=0A=20lisp/progmodes/project.el=20|=20=208=20+++++---=0A=20= lisp/replace.el=20=20=20=20=20=20=20=20=20=20=20|=2010=20++++++----=0A=20= 2=20files=20changed,=2011=20insertions(+),=207=20deletions(-)=0A=0Adiff=20= --git=20a/lisp/progmodes/project.el=20b/lisp/progmodes/project.el=0A= index=20c812f28c1b..b44b23554f=20100644=0A---=20= a/lisp/progmodes/project.el=0A+++=20b/lisp/progmodes/project.el=0A@@=20= -1072,9=20+1072,11=20@@=20project-query-replace-regexp=0A=20If=20you=20= exit=20the=20`query-replace',=20you=20can=20later=20continue=20the=0A=20= `query-replace'=20loop=20using=20the=20command=20\\[fileloop-continue]."=0A= =20=20=20(interactive=0A-=20=20=20(pcase-let=20((`(,from=20,to)=0A-=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20(query-replace-read-args=20= "Query=20replace=20(regexp)"=20t=20t)))=0A-=20=20=20=20=20(list=20from=20= to)))=0A+=20=20=20(let=20((read-regexp-defaults-function=20(lambda=20()=0A= +=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= (find-tag-default-as-regexp))))=0A+=20=20=20=20(pcase-let=20((`(,from=20= ,to)=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= (query-replace-read-args=20"Query=20replace=20(regexp)"=20t=20t)))=0A+=20= =20=20=20=20=20(list=20from=20to))))=0A=20=20=20= (fileloop-initialize-replace=0A=20=20=20=20from=20to=20(project-files=20= (project-current=20t))=20'default)=0A=20=20=20(fileloop-continue))=0A= diff=20--git=20a/lisp/replace.el=20b/lisp/replace.el=0Aindex=20= 60e507c642..3d7648611b=20100644=0A---=20a/lisp/replace.el=0A+++=20= b/lisp/replace.el=0A@@=20-208,7=20+208,8=20@@=20query-replace-read-from=0A= =20wants=20to=20replace=20FROM=20with=20TO."=0A=20=20=20(if=20= query-replace-interactive=0A=20=20=20=20=20=20=20(car=20(if=20= regexp-flag=20regexp-search-ring=20search-ring))=0A-=20=20=20=20(let*=20= ((history-add-new-input=20nil)=0A+=20=20=20=20(let*=20((sym-at-point=20= (symbol-at-point))=0A+=20=20=20=20=20=20=20=20=20=20=20= (history-add-new-input=20nil)=0A=20=09=20=20=20(separator-string=0A=20=09= =20=20=20=20(when=20query-replace-from-to-separator=0A=20=09=20=20=20=20=20= =20;;=20Check=20if=20the=20first=20non-whitespace=20char=20is=20= displayable=0A@@=20-234,8=20+235,9=20@@=20query-replace-read-from=0A=20=09= =20=20=20=20=20(symbol-value=20query-replace-from-history-variable)))=0A=20= =09=20=20=20(minibuffer-allow-text-properties=20t)=20;=20separator=20= uses=20text-properties=0A=20=09=20=20=20(prompt=0A-=09=20=20=20=20(cond=20= ((and=20query-replace-defaults=20separator)=0A-=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20(format-prompt=20prompt=20(car=20= minibuffer-history)))=0A+=09=20=20=20=20(cond=20(sym-at-point=20prompt)=20= ;;=20if=20there's=20a=20symbol=20at=20point,=20let=20read-regexp=20= format=20the=20prompt=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20((and=20query-replace-defaults=20separator)=0A+=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20(format-prompt=20prompt=20(car=20= minibuffer-history)))=0A=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20(query-replace-defaults=0A=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20(format-prompt=0A=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20prompt=20(format=20"%s=20->=20%s"=0A@@=20-255,7=20= +257,7=20@@=20query-replace-read-from=0A=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= (append=20'((separator=20.=20t)=20(face=20.=20t))=0A=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20text-property-default-nonsticky)))=0A=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20(if=20regexp-flag=0A-=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20(read-regexp=20prompt=20= nil=20'minibuffer-history)=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20(read-regexp=20prompt=20sym-at-point=20= 'minibuffer-history)=0A=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20(read-from-minibuffer=0A=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20prompt=20nil=20nil=20nil=20nil=0A=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20(query-replace-read-from-suggestions)=20= t)))))=0A--=20=0A2.34.1=0A=0A= --Apple-Mail=_2F99CCF6-1B02-4126-B774-97AE9DABC3A2--