Guest User

Untitled

a guest
May 1st, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.71 KB | None | 0 0
  1. ;; Move lines up/down
  2. (defun move-line-region-up (&optional start end n)
  3.   (interactive "r\np")
  4.   (if (use-region-p) (move-region-up start end n) (move-line-up n)))
  5.  
  6. (defun move-line-region-down (&optional start end n)
  7.   (interactive "r\np")
  8.   (if (use-region-p) (move-region-down start end n) (move-line-down n)))
  9.  
  10. (global-set-key (kbd "C-S-<up>") 'move-line-region-up)
  11. (global-set-key (kbd "C-S-<down>") 'move-line-region-down)
  12.  
  13. (defun move-region (start end n)
  14.   "Move the current region up or down by N lines."
  15.   (interactive "r\np")
  16.   (let ((line-text (delete-and-extract-region start end)))
  17.     (forward-line n)
  18.     (let ((start (point)))
  19.       (insert line-text)
  20.       (setq deactivate-mark nil)
  21.       (set-mark start))))
  22.  
  23. (defun move-region-up (start end n)
  24.   "Move the current line up by N lines."
  25.   (interactive "r\np")
  26.   (move-region start end (if (null n) -1 (- n))))
  27.  
  28. (defun move-region-down (start end n)
  29.   "Move the current line down by N lines."
  30.   (interactive "r\np")
  31.   (move-region start end (if (null n) 1 n)))
  32.  
  33. (defun move-line (n)
  34.   "Move the current line up or down by N lines."
  35.   (interactive "p")
  36.   (setq col (current-column))
  37.   (beginning-of-line) (setq start (point))
  38.   (end-of-line) (forward-char) (setq end (point))
  39.   (let ((line-text (delete-and-extract-region start end)))
  40.     (forward-line n)
  41.     (insert line-text)
  42.     ;; restore point to original column in moved line
  43.     (forward-line -1)
  44.     (forward-char col)))
  45.  
  46. (defun move-line-up (n)
  47.   "Move the current line up by N lines."
  48.   (interactive "p")
  49.   (move-line (if (null n) -1 (- n))))
  50.  
  51. (defun move-line-down (n)
  52.   "Move the current line down by N lines."
  53.   (interactive "p")
  54.   (move-line (if (null n) 1 n)))
Advertisement
Add Comment
Please, Sign In to add comment