all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* A strange issue with buffer-undo-list
@ 2021-02-23  5:19 Marcin Borkowski
  2021-02-24  0:43 ` Michael Heerdegen
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Marcin Borkowski @ 2021-02-23  5:19 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi all,

I was writing a command to move the current line past the next one (for
educational purposes), and I encountered a strange issue.

Here is my function:

--8<---------------cut here---------------start------------->8---
(defun move-line-down (count)
  "Move the current line down."
  (interactive "p")
  (let ((position-in-line (current-column)))
    (line-move 1)
    (transpose-lines count)
    (line-move -1)
    (move-to-column position-in-line)))
--8<---------------cut here---------------end--------------->8---

(It is definitely far from optimal, and I know that - the next version
will get rid of `transpose-lines' dependency and will just delete the
line below and recreate it above within a `save-excursion', which will
render the `position-in-line' stuff unnecessary.  But never mind this.)

So, my problem is that if I say M-x move-line-down, everything works,
but after undoing that change, the point lands at the bol of the next
line instead if where it had been at the beginning.  (I suppose this is
because only the `(transpose-lines count)' is effectively undone, so
`(line-move 1)` is not undone - that makes sense.)

So, I changed the code to this:

--8<---------------cut here---------------start------------->8---
(defun move-line-down (count)
  "Move the current line down."
  (interactive "p")
  (let ((position-in-line (current-column)))
    (push (point) buffer-undo-list)
    (line-move 1)
    (transpose-lines count)
    (line-move -1)
    (move-to-column position-in-line)))
--8<---------------cut here---------------end--------------->8---

which triggers an error.

Now, I did (of course) M-x toggle-debug-on-error, and so I learned about
"change groups".  I haven't heard about them before, so I consulted the
docs, and they seem cool (much like transactions in databases).  But
I have questions.

1. Why did my code confuse the change group mechanism?

2. How do I use `undo-amalgamate-change-group'?  The manual does not
provide any examples, and I only found one occurrence in the Emacs
source, and frankly, it didn't help a lot.  (Well, once I know the
answer to my next question, this will most probably be easy.)

3. Also, I am not even sure how the change group mechanism is supposed
to be used.  The docs tell me to use `unwind-protect', but this:

--8<---------------cut here---------------start------------->8---
(defun move-line-down (count)
  "Move the current line down."
  (interactive "p")
  (let ((position-in-line (current-column))
	(change-group (prepare-change-group)))
    (unwind-protect
	(progn
	  (activate-change-group change-group)
	  (line-move 1)
	  (transpose-lines count)
	  (line-move -1)
	  (move-to-column position-in-line)
	  (accept-change-group change-group))
      (cancel-change-group change-group))))
--8<---------------cut here---------------end--------------->8---

of course doesn't work, since both `accept-change-group' and
`cancel-change-group' are evaluated.  Is there a way to get the "state"
of the change group to only cancel it if it was not finished?

Anyway, it seems that this whole machinery is an overkill for the
purpose of just moving a line down - but as I said, I want to learn and
teach here, so this is kind of intentionally a toy example.

TIA,

-- 
Marcin Borkowski
http://mbork.pl



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

end of thread, other threads:[~2021-03-09 21:22 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-02-23  5:19 A strange issue with buffer-undo-list Marcin Borkowski
2021-02-24  0:43 ` Michael Heerdegen
2021-02-25  5:16   ` Marcin Borkowski
2021-02-25  5:28     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-02-25 14:19       ` Stefan Monnier
2021-02-25 15:58         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-03-08 21:43       ` Marcin Borkowski
2021-02-26  0:53     ` Michael Heerdegen
2021-03-08 21:42       ` Marcin Borkowski
2021-03-09  0:32         ` Michael Heerdegen
2021-03-09 21:22           ` Marcin Borkowski
2021-02-24  2:11 ` Michael Heerdegen
2021-02-24  3:33 ` Michael Heerdegen
2021-02-24  4:49   ` Stefan Monnier

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.