> On Jan 14, 2022, at 3:28 PM, Dmitry Gutov wrote: > > 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: >>> >>> let-bind read-regexp-defaults-function around the call >>> of query-replace-read-args, and use a symbol at point as the default. >>> >>> 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. > > 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. 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.