[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
point moved despite save-excursion, after deleting/reinserting region
From: |
Garreau, Alexandre |
Subject: |
point moved despite save-excursion, after deleting/reinserting region |
Date: |
Wed, 17 Oct 2018 03:43:16 +0200 |
User-agent: |
Gnus (5.13), GNU Emacs 25.1.1 (i686-pc-linux-gnu) |
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’m not
sure it’ll perfectly fix it the same.
Here the two functions (along with original kbd macro implementations,
not working because you can’t 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’t 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’t like “OMG WHATS HAPPANING”
(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
- point moved despite save-excursion, after deleting/reinserting region,
Garreau, Alexandre <=
- Re: point moved despite save-excursion, after deleting/reinserting region, Eli Zaretskii, 2018/10/17
- Re: point moved despite save-excursion, after deleting/reinserting region, Garreau, Alexandre, 2018/10/17
- Re: point moved despite save-excursion, after deleting/reinserting region, Eli Zaretskii, 2018/10/17
- Re: point moved despite save-excursion, after deleting/reinserting region, Garreau, Alexandre, 2018/10/18
- Re: point moved despite save-excursion, after deleting/reinserting region, Yuri Khan, 2018/10/18
- Re: point moved despite save-excursion, after deleting/reinserting region, Garreau, Alexandre, 2018/10/18
- Re: point moved despite save-excursion, after deleting/reinserting region, Eli Zaretskii, 2018/10/18
- Saving (and finding) markers (Was: Re: point moved despite save-excursion, after deleting/reinserting region), Garreau, Alexandre, 2018/10/18
- Re: Saving (and finding) markers (Was: Re: point moved despite save-excursion, after deleting/reinserting region), Eli Zaretskii, 2018/10/18
- Re: Saving (and finding) markers, Garreau, Alexandre, 2018/10/18