├── README.markdown └── duplicate-thing.el /README.markdown: -------------------------------------------------------------------------------- 1 | # README 2 | 3 | ## Name 4 | 5 | duplicate-thing 6 | 7 | ## Description 8 | 9 | *duplicate-thing.el* is Emacs lisp. Easy duplicate line or region, with comment out. 10 | 11 | 1. Duplicate current line. 12 | 2. Duplicate a selection when selection is active. 13 | 3. Only C-u, replicate, comment out the range. 14 | 4. Numerical prefix is specified as 'C-u 5': do multiple times repeatedly. 15 | 16 | ## Authors 17 | 18 | * ongaeshi 19 | 20 | ## License 21 | 22 | GPLv3 23 | 24 | ## Install 25 | 26 | ```emacs-lisp 27 | ;; auto-install 28 | (auto-install-from-url "https://raw.github.com/ongaeshi/duplicate-thing/master/duplicate-thing.el") 29 | ``` 30 | 31 | ## .emacs.d/init.el 32 | 33 | ```emacs-lisp 34 | (require 'duplicate-thing) 35 | (global-set-key (kbd "M-c") 'duplicate-thing) 36 | ``` 37 | 38 | ## Thanks 39 | 40 | * @a_ars: Idea, Can determine the difference between 'C-U' and 'C-U 4' 41 | * @k_somemo: Bug report 42 | -------------------------------------------------------------------------------- /duplicate-thing.el: -------------------------------------------------------------------------------- 1 | ;;; duplicate-thing.el --- Duplicate current line & selection 2 | 3 | ;; Copyright (C) 2012 ongaeshi 4 | 5 | ;; Author: ongaeshi 6 | ;; Keywords: convenience command duplicate line selection 7 | ;; URL: https://github.com/ongaeshi/duplicate-thing 8 | ;; Version: 0.2 9 | 10 | ;; This program is free software; you can redistribute it and/or modify 11 | ;; it under the terms of the GNU General Public License as published by 12 | ;; the Free Software Foundation, either version 3 of the License, or 13 | ;; (at your option) any later version. 14 | 15 | ;; This program is distributed in the hope that it will be useful, 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | ;; GNU General Public License for more details. 19 | 20 | ;; You should have received a copy of the GNU General Public License 21 | ;; along with this program. If not, see . 22 | 23 | ;;; Commentary: 24 | ;; 1. Duplicate current line. 25 | ;; 2. Duplicate a selection when selection is active. 26 | ;; 3. Only C-u, replicate, comment out the range. 27 | ;; 4. Numerical prefix is specified as 'C-u 5': do multiple times repeatedly. 28 | 29 | ;; (require 'duplicate-thing) 30 | ;; (global-set-key (kbd "M-c") 'duplicate-thing) 31 | 32 | ;;; Code: 33 | 34 | (defun duplicate-thing-line-start-after-forward-line-p () 35 | "Return 't if the position is beginning of line after foward-line." 36 | (forward-line) 37 | (= 0 (current-column))) 38 | 39 | (defun duplicate-thing-select-current-line () 40 | "Select current line." 41 | (let (start end) 42 | (beginning-of-line) 43 | (setq start (point)) 44 | (unless (duplicate-thing-line-start-after-forward-line-p) (newline)) 45 | (setq end (point)) 46 | (setq deactivate-mark nil) 47 | (set-mark start))) 48 | 49 | (defun duplicate-thing-expand-selection () 50 | "Expand selection to contain whole lines." 51 | (let ((start (region-beginning)) 52 | (end (region-end))) 53 | (goto-char start) 54 | (beginning-of-line) 55 | (setq start (point)) 56 | (goto-char end) 57 | (unless (= 0 (current-column)) 58 | (unless (duplicate-thing-line-start-after-forward-line-p) 59 | (newline))) 60 | (setq end (point)) 61 | (setq deactivate-mark nil) 62 | (set-mark start))) 63 | 64 | (defun duplicate-thing-at (p text n) 65 | "Duplicate TEXT N times at P." 66 | (dotimes (i (or n 1)) (insert text)) 67 | (set-mark p) 68 | (setq deactivate-mark nil)) 69 | 70 | ;;;###autoload 71 | (defun duplicate-thing (n) 72 | "Duplicate line or region N times. 73 | If it has active mark, it will expand the selection and duplicate it. 74 | If it doesn't have active mark, it will select current line and duplicate it." 75 | (interactive "P") 76 | (if mark-active 77 | (duplicate-thing-expand-selection) 78 | (duplicate-thing-select-current-line)) 79 | (let (p1 p2 len text with-comment-out) 80 | (setq p1 (region-beginning) 81 | p2 (region-end) 82 | len (- p2 p1) 83 | text (buffer-substring p1 p2) 84 | with-comment-out (consp n)) 85 | (if with-comment-out 86 | (progn 87 | (comment-region p1 p2) 88 | (duplicate-thing-at (point) text 1)) 89 | (duplicate-thing-at p2 text n))) 90 | (setq transient-mark-mode (cons 'only t))) 91 | 92 | (provide 'duplicate-thing) 93 | ;;; duplicate-thing.el ends here 94 | --------------------------------------------------------------------------------