From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: aklaing@gmail.com Newsgroups: gmane.emacs.help Subject: Re: How to return to the position from where I did tags-query-replace? Date: Sun, 4 Feb 2018 12:29:25 -0800 (PST) Message-ID: <4fc64263-c426-46b3-a0f6-eab78b203cdc@googlegroups.com> References: <1189017224.525081.173930@22g2000hsm.googlegroups.com> <2f9fe1c0-f382-47d4-8df2-0b39d3645385@googlegroups.com> <86d11lxwl9.fsf@zoho.com> <86y3k9wf58.fsf@zoho.com> <86mv0pweel.fsf@zoho.com> <34aa48a3-00b7-4adc-a103-474738a6cf57@googlegroups.com> 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 1517776189 16211 195.159.176.226 (4 Feb 2018 20:29:49 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 4 Feb 2018 20:29:49 +0000 (UTC) Injection-Date: Sun, 04 Feb 2018 20:29:25 +0000 User-Agent: G2/1.0 To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Feb 04 21:29:44 2018 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 1eiQv0-0003kR-NK for geh-help-gnu-emacs@m.gmane.org; Sun, 04 Feb 2018 21:29:42 +0100 Original-Received: from localhost ([::1]:36208 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eiQx0-0005hc-9H for geh-help-gnu-emacs@m.gmane.org; Sun, 04 Feb 2018 15:31:46 -0500 X-Received: by 10.200.50.16 with SMTP id x16mr7115665qta.9.1517776165727; Sun, 04 Feb 2018 12:29:25 -0800 (PST) X-Received: by 10.31.115.5 with SMTP id o5mr4677699vkc.12.1517776165496; Sun, 04 Feb 2018 12:29:25 -0800 (PST) Original-Path: usenet.stanford.edu!s47no7489089qta.0!news-out.google.com!u51ni87qtk.1!nntp.google.com!q21no274125qtn.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help In-Reply-To: <34aa48a3-00b7-4adc-a103-474738a6cf57@googlegroups.com> Complaints-To: groups-abuse@google.com Original-Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2601:183:c900:9ffa:e48a:7929:2e10:95b0; posting-account=dhIQkAoAAAC3SN9RXz6IZxkZKNcT7tos Original-NNTP-Posting-Host: 2601:183:c900:9ffa:e48a:7929:2e10:95b0 Original-Xref: usenet.stanford.edu gnu.emacs.help:221797 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:115914 Archived-At: Another minor note if anyone wants to use this: as suggested by Emanuel, yo= u will need to use these to put it in place (or remove it): (advice-add 'tags-query-replace :around #'do-tags-query-replace-return) ;; (advice-remove 'tags-query-replace #'do-tags-query-replace-return) On Sunday, February 4, 2018 at 3:14:39 PM UTC-5, akl...@gmail.com wrote: > Thanks Emanuel for taking a look at this. Your solution works correctly = for your test case (moving to a random buffer and location) but not as-writ= ten for tags-query-replace, which has some peculiarities (I just realized). >=20 > Looking at the code, the tags-query-replace function uses a function call= ed > user-error for flow control. Deep in the logic, if it realizes that it h= as finished the job normally, it calls user-error, which is somewhat simila= r to throwing an exception deep in the call stack. Because this unwinds th= e stack and discards it, the part of the code following (apply fun args) ne= ver gets evaluated. This probably also explains why my two initial attempt= s do not work. >=20 > One hacky solution (based on your approach) is to do something similar to= putting a try/catch around the call to (apply fun args), as follows: >=20 > (defun do-tags-query-replace-return (fun &rest args) > (let ((tmp-point (point))=20 > (tmp-buffer (current-buffer))) > (message "DWR Before: %d %s %s" tmp-point tmp-buffer args) > (condition-case tmp-err > (apply fun args) > (user-error > (let ((err-message (cadr tmp-err))) > (if (string=3D err-message "All files processed") > (message "DWR Trapped: %s" err-message) > (signal (car tmp-err) (cdr tmp-err)))))) > (message "DWR After: %d %s %s" tmp-point tmp-buffer args) > (when (bufferp tmp-buffer) > (switch-to-buffer tmp-buffer) > (goto-char tmp-point) > nil))) >=20 > Notes: this is similar to putting a try/catch around the > (apply fun args). I say it is (only) similar because elisp has a notion = of try/catch which is different from its own notion of error-handling, thou= gh > there are similarities. >=20 > I like your approach better than my first two attempts because it does no= t require an external package like breadcrumb. Though (I believe) register= s are native in emacs, it is better to avoid using persistent state for som= ething that should really be on the execution stack. >=20 > It should be noted that this solution only works for tags-query-replace, = because it only traps one very specific type of error, which is the one tag= s-query-replace "throws" when it completes normally. If tags-query-replace= exits for any other real error, this will fail -- in the sense that it wil= l not return to where it was before it started. And I think that is a good= thing. >=20 > One thing that is still not good about this solution is that if etags.el = changes the string that it uses to indicate completion ("All files processe= d"), this solution will stop working. That is too fragile. >=20 > In the long term, it would probably be better for etags.el to not use use= r-error to indicate normal completion, but that is more elisp than I want t= o do right now. It requires changes in more than one place. If the/an aut= hor of etags.el reads this and you have some time, maybe you can please tak= e a look? >=20 > If anyone wants to use the hacky solution above, you might as well remove= the DWR message calls. >=20 > Thanks again for your help, it pushed me to spend more time on it and lea= rn a bit more elisp than I already knew. >=20 > Thanks, >=20 > On Saturday, February 3, 2018 at 11:46:13 PM UTC-5, Emanuel Berg wrote: > > Emanuel Berg wrote: > >=20 > > > (goto-char (point)) > >=20 > > He he, good one! That should be > >=20 > > (goto-char point) > >=20 > > Any further edits will be in the source > > file [1] only. Modern society does not allow > > this inefficiency. > >=20 > > [1] http://user.it.uu.se/~embe8573/emacs-init/my-random.el > >=20 > > --=20 > > underground experts united > > http://user.it.uu.se/~embe8573