0

I want to be able to select multiple lines and move them up and down as a block, while "jumping" over the line in the direction of moving. Here's the code for moving up:

(defun on-ctrl-shift-up ()
    (move-selected-lines-up))

(defun move-selected-lines-up ()
    (let (
        (left-position (min (mark) (point)))
        (right-position (max (mark) (point))))

        (if (not (= (line-number-at-pos left-position) (line-number-at-pos (point-min))))
            (let ()
                (transpose-regions
                    (line-beginning-position (+ 1 (- (line-number-at-pos left-position) (line-number-at-pos (point)))))
                    (line-end-position (+ 1 (- (line-number-at-pos right-position) (line-number-at-pos (point)))))
                    (line-beginning-position (- (line-number-at-pos left-position) (line-number-at-pos (point))))
                    (line-end-position (- (line-number-at-pos left-position) (line-number-at-pos (point)))))))))
                    
(global-set-key (kbd "C-S-<up>") (lambda () (interactive) (on-ctrl-shift-up)))

So when I select two lines and I want to move them up, this is what I would want to get:

  • Item1
  • Item2
  • Item3
  • Item4

Upon pressing C-S-<up>:

  • Item2
  • Item3
  • Item1
  • Item4

But instead, I get this:

  • Item2
  • Item3
  • Item1
  • Item4

Meaning, the highlighted text gets cleared, so I can't repetitively press the shortcut, I have to select the two lines again and then press the shortcut. When I check after the command execution region-active-p and use-region-p, they are both true.

Why is this happening and how can I fix it?

2

1 Answer 1

0

You probably have transient-mark-mode turned on.

2
  • 1
    I don't think so, because it behaves in the same way, whether the transient-mark-mode is on or not.
    – MatX
    Commented Nov 19, 2023 at 17:00
  • Without transient-mark-mode, the region is not highlighted at all. It is on by default, so I suspect that @db48x is correct. See the doc string with C-h f transient-mark-mode to learn when the highlighting goes away (e.g. it does so when the buffer changes, as you have experienced). The highlighting comes back when the mark is reactivated.
    – NickD
    Commented Nov 20, 2023 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.