all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* point moved despite save-excursion, after deleting/reinserting region
@ 2018-10-17  1:43 Garreau, Alexandre
  2018-10-17 16:12 ` Eli Zaretskii
  0 siblings, 1 reply; 14+ messages in thread
From: Garreau, Alexandre @ 2018-10-17  1:43 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2018-10-19  6:38 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-10-17  1:43 point moved despite save-excursion, after deleting/reinserting region Garreau, Alexandre
2018-10-17 16:12 ` Eli Zaretskii
2018-10-17 20:13   ` Garreau, Alexandre
2018-10-18  2:38     ` Eli Zaretskii
2018-10-18  9:50       ` Garreau, Alexandre
2018-10-18 11:32         ` Yuri Khan
2018-10-18 11:45           ` Garreau, Alexandre
2018-10-18 13:16         ` Eli Zaretskii
2018-10-18 18:08           ` Saving (and finding) markers (Was: Re: point moved despite save-excursion, after deleting/reinserting region) Garreau, Alexandre
2018-10-18 18:32             ` Eli Zaretskii
2018-10-18 19:36               ` Saving (and finding) markers Garreau, Alexandre
2018-10-18 20:11                 ` Eli Zaretskii
2018-10-19  1:15               ` Stefan Monnier
2018-10-19  6:38                 ` Eli Zaretskii

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.