emacsfodder/move-text

Broken for emacs 25

Closed this issue ยท 18 comments

Moving region works as expected, but moving lines is broken in emacs 25.

I was using another package for this but am finding it annoying. (Name escapes me now.)

Anyway, I will fix this up soon, because I'll be using it again on Emacs 25.

I'm using this to get the same functionality :-

(defun move-line-up ()
  (interactive)
  (transpose-lines 1)
  (forward-line -2))

(defun move-line-down ()
  (interactive)
  (forward-line 1)
  (transpose-lines 1)
  (forward-line -1))

(defun move-region (start end n)
  "Move the current region up or down by N lines."
  (interactive "r\np")
  (let ((line-text (delete-and-extract-region start end)))
    (forward-line n)
    (let ((start (point)))
      (insert line-text)
      (setq deactivate-mark nil)
      (set-mark start))))

(defun move-region-up (start end n)
  "Move the current line up by N lines."
  (interactive "r\np")
  (move-region start end (if (null n) -1 (- n))))

(defun move-region-down (start end n)
  "Move the current line down by N lines."
  (interactive "r\np")
  (move-region start end (if (null n) 1 n)))

(defun move-line-region-up (&optional start end n)
  (interactive "r\np")
  (if (use-region-p) (move-region-up start end n) (move-line-up)))

(defun move-line-region-down (&optional start end n)
  (interactive "r\np")
  (if (use-region-p) (move-region-down start end n) (move-line-down)))

I do not remember where i got this from though

Yes. works perfectly fine for emacs25

Ok, so that's probably miscommunication on my part:

Please place a line of text at the end of a buffer, e.g.

buffer ... blah blah..



text right on the last line, no blanks following[ place cursor here ]

Now try to use M-x move-line-region-up, either with or without the region selected, you'll notice it doesn't actually work.

It also bears asking, have you tried this code with Emacs 24?

As there's no specific code for dealing with versions 24-25 I will assume that it doesn't.

Note: a bit late here, and I think my testing is off! Apologies! I'll do some confirmation of results on Emacs -Q in the morning.

Note: a bit late here, and I think my testing is off! Apologies! I'll do some confirmation of results on Emacs -Q in the morning.

Retested: Behavior on bottom line is a little odd, seems that a blank line is created and that is moved instead of the actual line.

Definitely want to clean this up and add conditional behavior when we're on the last line.

Top line behavior is odd too.

To my mind:

  • if you're at the top, you cannot move the top line up. (it should be able to move down, as normal)
  • if you're at the bottom, you cannot move the bottom line down. (it should be able to move up, as normal)

Yes, the top and bottom line behaviour is broken. When at the last line, executing move-line-region-down will move the current line to the next line. (We'd want to disable this) and when at the top line, executingmove-line-region-up will cause the top line to be moved down (but, I'd never really do this in everyday usage)

It also bears asking, have you tried this code with Emacs 24?

I have not tested this in emacs 24. I used your package when I was using emacs 24, after moving to emacs 25, I discovered it was broken and found this piece of code from someones emacs config file.

Also, just curious, why did your package stop working for emacs 25 ?? Did emacs25 change the behaviour of any of the functions which broke the functionality ?

Right now I have forgotten the specifics, on phone.

But the gist of it was that a function behaved differently.

There are things that changed in 24 - 25 that affect a few packages, for example save-excursion used to leave the region/mark alone, but now you must use save-mark-and-excursion. I know that's not the function at play for this problem, but it's just to illustrate that there are a few underlying functions that suddenly stopped working as they did before.

Please note that 2.0.0 has just been pushed and should be on MELPA (soon)

I've decided to deprecate support for Emacs <= 24

Move text will only support Emacs >= 25

The code is based on the code you posted @CSRaghunandan , with additional handling added for first line and last line support.

Cool. Thanks for your effort ๐Ÿ‘ I'll check when it's up on Melpa

Got the update from Melpa. Looks like everything is fine except it allows for moving the text down even when at the last line. The bug when moving a line at the top of the file is fixed.

Updated to move-text stable 2.0.3 and fixed last line and penultimate line issues.

#6 for more details on fix

v2.0.5 (stable) on MELPA / MELPA stable soon.