Last active
August 26, 2024 02:14
-
-
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. 選択範囲がある場合は選択範囲を、選択範囲がない場合は、行を複製します。繰り返した場合、同じ文字列を複製し続けます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; 行/選択範囲の複製 (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 the region if active, otherwise duplicate the current line." | |
(interactive) | |
(let* ((region-active (region-active-p)) | |
(is-repeat (eq last-command this-command)) | |
(beg (if region-active (region-beginning) (line-beginning-position))) | |
(end (if region-active (region-end) (line-end-position))) | |
(strings (buffer-substring-no-properties beg end)) | |
(is-line (if is-repeat | |
previous-duplicate-region-or-line-was-line | |
(not region-active)))) | |
;; Adjust strings for line duplication | |
(when is-line | |
(setq strings (concat "\n" strings)) | |
(end-of-line)) | |
;; Insert the duplicated text | |
(if is-repeat | |
(insert previous-duplicate-region-or-line) | |
(insert strings) | |
(setq previous-duplicate-region-or-line strings)) | |
;; Store whether it was a line duplication | |
(setq previous-duplicate-region-or-line-was-line is-line))) | |
(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