切换相邻线的部分

时间:2014-04-09 18:56:13

标签: emacs elisp

我有这个代码可以向上/向下切换行:

;; Moving a line up or down
(defun move-text-internal (arg)
  (cond
   ((and mark-active transient-mark-mode)
    (if (> (point) (mark))
        (exchange-point-and-mark))
    (let ((column (current-column))
          (text (delete-and-extract-region (point) (mark))))
      (forward-line arg)
      (move-to-column column t)
      (set-mark (point))
      (insert text)
      (exchange-point-and-mark)
      (setq deactivate-mark nil)))
   (t
    (let ((column (current-column)))
      (beginning-of-line)
      (when (or (> arg 0) (not (bobp)))
        (forward-line)
        (when (or (< arg 0) (not (eobp)))
          (transpose-lines arg))
        (forward-line -1))
      (move-to-column column t)))))

(defun move-text-down (arg)
  "Move region (transient-mark-mode active) or current line
  arg lines down."
  (interactive "*p")
  (move-text-internal arg))
(global-set-key [M-S-down] 'move-text-down)

(defun move-text-up (arg)
  "Move region (transient-mark-mode active) or current line
  arg lines up."
  (interactive "*p")
  (move-text-internal (- arg)))
(global-set-key [M-S-up]   'move-text-up)

我想知道是否可以调整move-text-internal函数,因此可以在光标&#34;之后移动部分行&#34;向上或向下。

以下是一个例子:

在:

A X B W
Q E O P

如果光标位于第一行的X之后,则在M-S-down之后:

A X O P
Q E B W

更新: 感谢Jordan Biondo的代码和功能。 只要你继续调用命令,我就会调整它以保持线条移动。

(defun flip-text (&optional arg)
  "Flip the text from point to the end of the current line with the text
in the next line from the same column to the end of the next line.
With a prefix arg, flip text with the line above the current."
  (interactive "p")
  (save-excursion
    (let ((tt (delete-and-extract-region (point) (point-at-eol)))
          (c (current-column)))
      (forward-line arg)
      (move-to-column c)
      (insert tt)
      (let ((ot (delete-and-extract-region (point) (point-at-eol))))
        (forward-line (- arg))
        (goto-char (point-at-eol))
        (insert ot)
    ))
    )
  (previous-line (- arg))
  )
(global-set-key (kbd "C-M-z") (lambda ()
                  (interactive)
                  (flip-text 1)))
(global-set-key (kbd "C-M-c") (lambda ()
                  (interactive)
                  (flip-text -1)))

1 个答案:

答案 0 :(得分:3)

这将执行您指定但不执行多行的操作。

(defun flip-text-to-eol (&optional up)
  "Flip the text from point to the end of the current line with the text
in the next line from the same column to the end of the next line.

With a prefix arg, flip text with the line above the current."
  (interactive "P")
  (save-excursion
    (let ((tt (delete-and-extract-region (point) (point-at-eol)))
          (c (current-column)))
      (forward-line (if up -1 1))
      (move-to-column c)
      (insert tt)
      (let ((ot (delete-and-extract-region (point) (point-at-eol))))
        (forward-line (if up 1 -1))
        (goto-char (point-at-eol))
        (insert ot)))))

enter image description here