[Top][All Lists]

[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)


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

(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)
        (search-forward-regexp "\\<[_[:alpha:]][_0-9[:alpha:]]*\\>" bound)
        (let ((var-value (read-string (format "Enter value for %s: " 
(match-string 0)))))
          (delete-region (mark) (point))
          (insert (sql-obj-string var-value))

(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
    (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”
      (while (src-arg prefix end))
      (let (org-confirm-babel-evaluate)
      (save-restriction       ;; these may be optional,
        (org-narrow-to-block) ;; see before
        (delete-region (point-min) (point-max))
        (insert orig)))))


reply via email to

[Prev in Thread] Current Thread [Next in Thread]