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:14:36 -0800 (PST) Message-ID: <34aa48a3-00b7-4adc-a103-474738a6cf57@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> 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 1517775211 25658 195.159.176.226 (4 Feb 2018 20:13:31 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 4 Feb 2018 20:13:31 +0000 (UTC) Injection-Date: Sun, 04 Feb 2018 20:14:37 +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:13:27 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 1eiQfF-0006Jj-Jk for geh-help-gnu-emacs@m.gmane.org; Sun, 04 Feb 2018 21:13:25 +0100 Original-Received: from localhost ([::1]:34621 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eiQhG-0005gT-GU for geh-help-gnu-emacs@m.gmane.org; Sun, 04 Feb 2018 15:15:30 -0500 X-Received: by 10.237.62.149 with SMTP id n21mr2010079qtf.61.1517775277749; Sun, 04 Feb 2018 12:14:37 -0800 (PST) X-Received: by 10.31.3.215 with SMTP id f84mr4207146vki.9.1517775277350; Sun, 04 Feb 2018 12:14:37 -0800 (PST) Original-Path: usenet.stanford.edu!s47no7482262qta.0!news-out.google.com!u51ni83qtk.1!nntp.google.com!s47no7482257qta.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help In-Reply-To: <86mv0pweel.fsf@zoho.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:221795 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:115912 Archived-At: Thanks Emanuel for taking a look at this. Your solution works correctly fo= r your test case (moving to a random buffer and location) but not as-writte= n for tags-query-replace, which has some peculiarities (I just realized). Looking at the code, the tags-query-replace function uses a function called user-error for flow control. Deep in the logic, if it realizes that it has= finished the job normally, it calls user-error, which is somewhat similar = to throwing an exception deep in the call stack. Because this unwinds the = stack and discards it, the part of the code following (apply fun args) neve= r gets evaluated. This probably also explains why my two initial attempts = do not work. One hacky solution (based on your approach) is to do something similar to p= utting a try/catch around the call to (apply fun args), as follows: (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))) 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, though there are similarities. I like your approach better than my first two attempts because it does not = require an external package like breadcrumb. Though (I believe) registers = are native in emacs, it is better to avoid using persistent state for somet= hing that should really be on the execution stack. It should be noted that this solution only works for tags-query-replace, be= cause it only traps one very specific type of error, which is the one tags-= query-replace "throws" when it completes normally. If tags-query-replace e= xits for any other real error, this will fail -- in the sense that it will = not return to where it was before it started. And I think that is a good t= hing. One thing that is still not good about this solution is that if etags.el ch= anges the string that it uses to indicate completion ("All files processed"= ), this solution will stop working. That is too fragile. In the long term, it would probably be better for etags.el to not use user-= error to indicate normal completion, but that is more elisp than I want to = do right now. It requires changes in more than one place. If the/an autho= r of etags.el reads this and you have some time, maybe you can please take = a look? If anyone wants to use the hacky solution above, you might as well remove t= he DWR message calls. Thanks again for your help, it pushed me to spend more time on it and learn= a bit more elisp than I already knew. Thanks, 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