From 88248111d11bdcdf167112b9fb9a39026c45e5a3 Mon Sep 17 00:00:00 2001 From: Bozhidar Batsov Date: Sat, 13 Jul 2013 14:15:16 +0300 Subject: [PATCH] Add prelude-duplicate-and-comment-current-line-or-region (fixes #348) --- README.md | 1 + core/prelude-core.el | 24 ++++++++++++++++++++++++ core/prelude-mode.el | 1 + 3 files changed, 26 insertions(+) diff --git a/README.md b/README.md index 85488dc..1accf0f 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,7 @@ Keybinding | Description C-c e | Eval a bit of Emacs Lisp code and replace it with its result. C-c s | Swap two active windows. C-c d | Duplicate the current line (or region). +C-c M-d | Duplicate and comment the current line (or region). C-c r | Rename the currently visited file and buffer. C-c t | Open a terminal emulator (`ansi-term`). C-c k | Kill all open buffers except the one you're currently in. diff --git a/core/prelude-core.el b/core/prelude-core.el index b2402b3..6aed171 100644 --- a/core/prelude-core.el +++ b/core/prelude-core.el @@ -222,6 +222,30 @@ there's a region, all lines that region covers will be duplicated." (setq end (point)))) (goto-char (+ origin (* (length region) arg) arg))))) +;; TODO: Remove code duplication by extracting something more generic +(defun prelude-duplicate-and-comment-current-line-or-region (arg) + "Duplicates and comments the current line or region ARG times. +If there's no region, the current line will be duplicated. However, if +there's a region, all lines that region covers will be duplicated." + (interactive "p") + (let (beg end (origin (point))) + (if (and mark-active (> (point) (mark))) + (exchange-point-and-mark)) + (setq beg (line-beginning-position)) + (if mark-active + (exchange-point-and-mark)) + (setq end (line-end-position)) + (let ((region (buffer-substring-no-properties beg end))) + (comment-or-uncomment-region beg end) + (setq end (line-end-position)) + (-dotimes arg + (lambda (n) + (goto-char end) + (newline) + (insert region) + (setq end (point)))) + (goto-char (+ origin (* (length region) arg) arg))))) + (defun prelude-rename-file-and-buffer () "Renames current buffer and file it is visiting." (interactive) diff --git a/core/prelude-mode.el b/core/prelude-mode.el index 7c64762..70cd31f 100644 --- a/core/prelude-mode.el +++ b/core/prelude-mode.el @@ -54,6 +54,7 @@ (define-key map (kbd "C-c s") 'prelude-swap-windows) (define-key map (kbd "C-c D") 'prelude-delete-file-and-buffer) (define-key map (kbd "C-c d") 'prelude-duplicate-current-line-or-region) + (define-key map (kbd "C-c M-d") 'prelude-duplicate-and-comment-current-line-or-region) (define-key map (kbd "C-c r") 'prelude-rename-file-and-buffer) (define-key map (kbd "C-c t") 'prelude-visit-term-buffer) (define-key map (kbd "C-c k") 'prelude-kill-other-buffers)