From 280abe85bb2ecdbac5f427bd44871c2ad716a985 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Tue, 17 Aug 2021 11:04:27 +0800 Subject: new function: durand-copy-line-dwim * basic.el (global-map): Bind to "s-;". * common.el (durand-copy-line-dwim): This meets my needs to copy or duplicate lines. --- basic.el | 4 ++++ common.el | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/basic.el b/basic.el index e23688d..cd5fdff 100644 --- a/basic.el +++ b/basic.el @@ -209,6 +209,10 @@ This will maintain the frame's width and height as well." (display-buffer-in-tab durand-display-in-one-window) (tab-name . "man page")))) +;;; copy-duplicate dwim + +(define-key global-map (vector ?\s-\;) #'durand-copy-line-dwim) + ;;; Custom kill buffer function ;;;###autoload diff --git a/common.el b/common.el index 0417da5..75fe201 100644 --- a/common.el +++ b/common.el @@ -393,6 +393,63 @@ Comparison is done by TEST if that is non-nil, else by `equal'." ((setq temp (cdr temp))))) temp)) +;;;###autoload +(defun durand-copy-line-dwim (&optional arg) + "Copy line or duplicate line, according to ARG. +If ARG is nil, then duplicate line and put cursor in the line +below. + +If ARG is '(4), just copy line. + +If ARG is '(16), copy and duplicate line at the same time. + +If ARG is a number strictly greater than 0, then copy and +duplicate the line ARG times. + +In any other case, copy line and use minibuffer to read a number, +and duplicate the line at the line that is ARG away from the +current line; negative ARG means to put the line upwards." + (interactive "P") + (cond + ((null arg) + (let ((line (buffer-substring (line-beginning-position) + (line-end-position)))) + (goto-char (line-end-position)) + (insert "\n" line))) + ((equal arg (list 4)) + (let ((line (concat (buffer-substring (line-beginning-position) + (line-end-position)) + "\n"))) + (kill-new line) + (message "Copied \"%s\"" (substring-no-properties line)))) + ((equal arg (list 16)) + (let ((line (buffer-substring (line-beginning-position) + (line-end-position)))) + (kill-new (concat line "\n")) + (message "Copied \"%s\n\"" (substring-no-properties line)) + (goto-char (line-end-position)) + (insert "\n") + (insert line))) + ((and (integerp arg) + (> arg 0)) + (let* ((line (buffer-substring (line-beginning-position) + (line-end-position))) + (lines (mapconcat (lambda (_arg) line) + (number-sequence 1 arg) + "\n"))) + (kill-new (concat line "\n")) + (message "Copied \"%s\n\"" (substring-no-properties line)) + (insert "\n" lines))) + (t + (let ((line (buffer-substring (line-beginning-position) + (line-end-position))) + (num (read-number "Duplicate to which line? "))) + (kill-new (concat line "\n")) + (message "Copied \"%s\n\"" (substring-no-properties line)) + (goto-char (line-end-position (cond ((/= num 0) (1+ num)) + (num)))) + (insert "\n" line))))) + (provide 'common) ;;; common.el ends here. -- cgit v1.2.3-18-g5258