From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: "Garreau\, Alexandre" Newsgroups: gmane.emacs.help Subject: point moved despite save-excursion, after deleting/reinserting region Date: Wed, 17 Oct 2018 03:43:16 +0200 Message-ID: <87r2gpic0b.fsf@portable.galex-713.eu> 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 1539740510 11044 195.159.176.226 (17 Oct 2018 01:41:50 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 17 Oct 2018 01:41:50 +0000 (UTC) User-Agent: Gnus (5.13), GNU Emacs 25.1.1 (i686-pc-linux-gnu) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Oct 17 03:41:46 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 1gCaqE-0002l8-Cf for geh-help-gnu-emacs@m.gmane.org; Wed, 17 Oct 2018 03:41:42 +0200 Original-Received: from localhost ([::1]:33343 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gCasK-0007wt-6C for geh-help-gnu-emacs@m.gmane.org; Tue, 16 Oct 2018 21:43:52 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:45543) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gCars-0007wd-JI for help-gnu-emacs@gnu.org; Tue, 16 Oct 2018 21:43:25 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gCarp-0001Xc-T3 for help-gnu-emacs@gnu.org; Tue, 16 Oct 2018 21:43:23 -0400 Original-Received: from portable.galex-713.eu ([2a00:5884:8305::1]:41666) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gCarn-0001PG-BV for help-gnu-emacs@gnu.org; Tue, 16 Oct 2018 21:43:21 -0400 Original-Received: from localhost ([::1] helo=portable.galex-713.eu) by portable.galex-713.eu with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gCark-0002HF-S8 for help-gnu-emacs@gnu.org; Wed, 17 Oct 2018 03:43:17 +0200 X-GPG-FINGERPRINT: E109 9988 4197 D7CB B0BC 5C23 8DEB 24BA 867D 3F7F X-Accept-Language: fr, en, it, eo X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:5884:8305::1 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:118292 Archived-At: Hi, I spent some time on one or two functions that modified a org source block, evaled it, and restored it as before the modification: after narrowing, I saved it using buffer-substring, and restored it using delete-region then insert what I saved. However doing that my point ends either at the beginning or the end of what I inserted, even after save-excursion is over. How to fix that? I fill the simple way of restoring changes, without loosing what evaluation did print outside of narrow (nor exiting this restriction so that to save the result of evaluation, undo, and insert it back), is indeed to delete/reinsert everything. It would be too complex (though feasible) to store each individual change along with the position and length of it, to restore it back afterwards, and I=E2=80=99m n= ot sure it=E2=80=99ll perfectly fix it the same. Here the two functions (along with original kbd macro implementations, not working because you can=E2=80=99t nest 0-prefix loops in macros): #+BEGIN_SRC emacs-lisp (fset 'src-arg [?\C-s ?: ?\C-m ?\C-? ?\C- ?\C-\M-s ?\\ ?< ?\[ ?_ ?\[ ?: ?a ?l ?p ?h ?a ?: ?\] ?\] ?\[ ?_ ?0 ?- ?9 ?\[ ?: ?a ?l ?p ?h ?a ?: ?\] ?\] ?* ?\\ ?> ?\C-m ?\C-u ?\M-x ?r ?e ?a ?d ?- ?v ?a ?r ?\C-m ?\C-u ?\C-x ?q ?\C-m]) (defun src-arg (&optional prefix bound) "Search until BOUND for argument to replace for a prompted value. With 0 prefix argument, repeat until search fails." (interactive "p") (if (zerop prefix) (while (src-arg nil bound)) (when-let ((search (search-forward ":" bound t))) (prog1 search (delete-char -1) (push-mark) (search-forward-regexp "\\<[_[:alpha:]][_0-9[:alpha:]]*\\>" bound) (activate-mark) (let ((var-value (read-string (format "Enter value for %s: " (match= -string 0))))) (delete-region (mark) (point)) (insert (sql-obj-string var-value)) (pop-mark)))))) (fset 'src-args [?\C-x ?n ?b ?\M-> ?\C- ?\M-< ?\M-w ?\C-n ?\C-u ?0 ?\M-x ?s ?r ?c ?- ?a ?r ?g return ?\C-c ?\C-c ?y ?e ?s return ?\M-< ?\C-y ?\C- ?\M-> ?\C-w ?\C-a ?\C-x ?n ?w]) (defun src-args (&optional prefix) "" (interactive "P") (save-mark-and-excursion ;; doesn=E2=80=99t work (org-narrow-to-block) (let* ((end (point-max)) (orig (buffer-substring (goto-char (point-min)) end))) (widen) ;; optional, so that the user still see the rest of ;; buffer and isn=E2=80=99t like =E2=80=9COMG WHATS HAPPANING= =E2=80=9D (forward-line) (while (src-arg prefix end)) (let (org-confirm-babel-evaluate) (org-ctrl-c-ctrl-c)) (save-restriction ;; these may be optional, (org-narrow-to-block) ;; see before (delete-region (point-min) (point-max)) (insert orig))))) #+END_SRC