Subscribed unsubscribe Subscribe Subscribe

Ctrl+up/downでカーソルと一緒に領域を動かす

Emacs

修正版があります

昨日のやつと合わせるとctrl+up/downでregionを動かせます。

(defun line-number-at-mark ()
  (let ((p (point))
        (result nil)
        (m nil))
    (setq m (mark))
    (cond ((null m) nil)
          (t (goto-char m)
             (setq result (line-number-at-pos))
             (goto-char p)
             result))))

(defun push-mark-line (num)
  (let ((p (point)))
    (goto-line num)
    (push-mark)
    (goto-char p)))

(defun move-region-down ()
  (interactive)
  (let ((lp (line-number-at-pos))
        (lm (line-number-at-mark))
        (tmp nil))
    (cond ((> lm lp)
           (setq tmp lm)
           (setq lm lp)
           (setq lp tmp)))
    (goto-line (1+ lp))
    (dotimes (i (- lp lm -1))
      (exchange-lines-up))
    (goto-line (1+ lp))
    (push-mark-line (1+ lm))))

(defun move-region-up ()
  (interactive)
  (let ((lp (line-number-at-pos))
        (lm (line-number-at-mark))
        (tmp nil))
    (cond ((< lm lp)
           (setq tmp lm)
           (setq lm lp)
           (setq lp tmp)))
    (if (= lp 1)
        nil
      (goto-line (1- lp))
      (dotimes (i (- lm lp -1))
        (exchange-lines-down))
      (goto-line (1- lp))
      (push-mark-line (1- lm)))))

(global-set-key [(control up)] 'move-region-up)
(global-set-key [(control down)] 'move-region-down)