Skip to content

Instantly share code, notes, and snippets.

@jidaikobo-shibata
Last active August 3, 2016 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jidaikobo-shibata/297fe973cde66b384fa1 to your computer and use it in GitHub Desktop.
Save jidaikobo-shibata/297fe973cde66b384fa1 to your computer and use it in GitHub Desktop.
Emacs(Elisp): duplicate region or line. if same command repeated, then duplicate sate strings. 選択範囲がある場合は選択範囲を、選択範囲がない場合は、行を複製します。繰り返した場合、同じ文字列を複製し続けます。
;;; 行/選択範囲の複製 (cmd+d)
;; gist-description: Emacs(Elisp): duplicate region or line. if same command repeated, then duplicate sate strings. 選択範囲がある場合は選択範囲を、選択範囲がない場合は、行を複製します。繰り返した場合、同じ文字列を複製し続けます。
;; gist-id: 297fe973cde66b384fa1
;; gist-name: duplicate-region-or-line.el
;; gist-private: nil
(defvar previous-duplicate-region-or-line nil)
(defvar previous-duplicate-region-or-line-was-line nil)
(defun duplicate-region-or-line ()
"Duplicate region or line."
(interactive)
(let* ((beg (if mark-active (region-beginning) (line-beginning-position)))
(end (if mark-active (region-end) (line-end-position)))
(strings (buffer-substring-no-properties beg end))
(is-repeat (eq last-command this-command))
(is-line (if is-repeat
previous-duplicate-region-or-line-was-line
(not (region-active-p))))
(strings (if (region-active-p)
strings
(concat "\n" strings))))
(if is-line
(progn
(end-of-line)
(setq previous-duplicate-region-or-line-was-line is-line))
(setq previous-duplicate-region-or-line-was-line nil))
(if is-repeat
(insert previous-duplicate-region-or-line)
(insert strings)
(setq previous-duplicate-region-or-line strings))))
(global-set-key (kbd "s-d") 'duplicate-region-or-line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment