From d2517ef2ae389e79a35d012c239b4c5ef63076ab Mon Sep 17 00:00:00 2001 From: Ravi R Kiran Date: Sun, 1 Dec 2013 22:39:42 -0800 Subject: Add move-lines for moving regions and lines Add wgrep for writable grep buffers diff --git a/init.el b/init.el index 1b5a64d..429967e 100644 --- a/init.el +++ b/init.el @@ -32,6 +32,7 @@ ;("marmalade" . "http://marmalade-repo.org/packages/") ("melpa" . "http://melpa.milkbox.net/packages/"))) (add-to-list 'load-path ravi/init-dir) +(add-to-list 'load-path (ravi/emacs-file "site-lisp/")) (setq autoload-file (concat ravi/init-dir "loaddefs.el")) (setq package-user-dir (concat ravi/init-dir "elpa")) diff --git a/ravi-init-navigation.el b/ravi-init-navigation.el index a77157b..f401273 100644 --- a/ravi-init-navigation.el +++ b/ravi-init-navigation.el @@ -1,9 +1,9 @@ ;;; ravi-init-navigation.el --- navigation utilities -;; Copyright (C) 2013 +;; Copyright (C) 2013 ;; Author: -;; Keywords: +;; Keywords: ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by @@ -34,9 +34,47 @@ :ensure t ) +(use-package expand-region + :bind ("C-=" . er/expand-region) + :ensure t + ) + +(use-package multiple-cursors + :bind (("C-S-c C-S-c" . mc/edit-lines) + + ("C->" . mc/mark-next-like-this) + ("C-<" . mc/mark-previous-like-this) + ("C-c C-<" . mc/mark-all-like-this)) + :init + (setq mc/list-file (ravi/emacs-file "past/mc-lists.el")) + ) + (bind-key "" 'ff-find-other-file) (setq compilation-scroll-output 'first-error) +;; Marking and moving lines + +(bind-key + "M-j" + (lambda () + (interactive) + (join-line -1)) + ) + +(use-package move-lines + :bind (("" . move-lines-down) + ("" . move-lines-up) + ) + ) + +;; grep and friends + +(use-package wgrep + :ensure t + ) + + + (provide 'ravi-init-navigation) ;;; ravi-init-navigation.el ends here diff --git a/site-lisp/move-lines.el b/site-lisp/move-lines.el new file mode 100644 index 0000000..f96c598 --- /dev/null +++ b/site-lisp/move-lines.el @@ -0,0 +1,128 @@ +;;; move-lines.el --- move current line or lines surrounding region up or down + +;; Copyright (C) 2013 Emanuele Tomasi +;; +;; This program is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. +;; +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. +;; +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary; + +;; There are two entry points: `move-lines-up' moves the text up and +;; `move-lines-down' that moves the text down. +;; +;; Copy this file in a directory which is in the Emacs `load-path'. Then, +;; execute the following code either directly or in your .emacs file: +;; +;; (require 'move-lines) +;; (move-lines-binding) +;; +;; Now, you can move the line(s) up by M-p or M- or down by M-n or M-. + +;;; Code: + +(defun move-lines--internal (n) + (let* ((start (point)) ;; The position of beginning of line of the first line + (end start) ;; The position of eol+\n of the end line + col-init ;; The current column for the first line + (col-end (current-column)) ;; The current column for the end line + exchange_pm ;; If I had exchanged point and mark + delete-latest-newline) ;; If I had inserted a newline at the end + + ;; STEP 1: Identifying the line(s) to cut. + ;; --- + ;; If region is actives, I ensure that point always is at the end of the + ;; region and mark at the beginning. + (when (region-active-p) + (when (< (point) (mark)) + (setq exchange_pm t) + (exchange-point-and-mark)) + (setq start (mark) + end (point) + col-end (current-column))) + + (goto-char start) (setq col-init (current-column)) + (beginning-of-line) (setq start (point)) + + (goto-char end) (end-of-line) + ;; If point == point-max, this buffers doesn't have the trailing newline. + ;; In this case I have to insert a newline otherwise the following + ;; `forward-char' (to keep the "\n") will fail. + (when (= (point) (point-max)) + (setq delete-latest-newline t) + (insert-char ?\n) (forward-char -1)) + (forward-char 1) (setq end (point)) + + ;; STEP 2: Moving the lines. + ;; --- + ;; The region I'm cutting span from the beginning of line of the current + ;; line (or current region) to the end of line + 1 (newline) of the current + ;; line (or current region). + (let ((line-text (delete-and-extract-region start end))) + (forward-line n) + ;; If the current-column != 0, I have moved the region at the bottom of a + ;; buffer doesn't have the trailing newline. + (when (not (= (current-column) 0)) + (insert-char ?\n) + (setq delete-latest-newline t)) + (setq start (+ (point) col-init)) ;; Now, start is the start of new region + (insert line-text)) + + ;; STEP 3: Restoring + ;; --- + ;; I'm at the end of new region (or line) and start has setted at the + ;; beginning of new region (if a region is active). + ;; Restoring the end column. + (forward-line -1) + (forward-char col-end) + + (when delete-latest-newline + (save-excursion + (goto-char (point-max)) + (delete-char -1))) + + (when (region-active-p) + (setq deactivate-mark nil) + (set-mark start) + (if exchange_pm + (exchange-point-and-mark))))) + +;;;###autoload +(defun move-lines-up (n) + "Moves the current line or, if region is actives, the lines surrounding +region, up by N lines, or 1 line if N is nil." + (interactive "p") + (if (eq n nil) + (setq n 1)) + (move-lines--internal (- n))) + +;;;###autoload +(defun move-lines-down (n) + "Moves the current line or, if region is actives, the lines surrounding +region, down by N lines, or 1 line if N is nil." + (interactive "p") + (if (eq n nil) + (setq n 1)) + (move-lines--internal n)) + +;;;###autoload +(defun move-lines-binding () + "Sets the default key binding for moving lines. M-p or M- for moving up +and M-n or M- for moving down." + (global-set-key (kbd "M-p") 'move-lines-up) + (global-set-key (kbd "M-") 'move-lines-up) + (global-set-key (kbd "M-n") 'move-lines-down) + (global-set-key (kbd "M-") 'move-lines-down)) + +(provide 'move-lines) + +;; move-lines.el ends here -- cgit v0.10.1